JUnit5: Trouble Importing Assertions - java

I'm trying to use JUnit5 to create some basic unit tests. I go to my Analyzer.java class and get the popup for creating a test. I hit Create New Test, setting Testing Library to JUnit5. I check off a bunch of methods to generate test methods for and hit OK.
So now I have an AnalyzerTest.java file and at the top I have:
import static org.junit.jupiter.api.Assertions.*;
Unfortunately, Assertions is red (this is in IntelliJ IDEA). When I hover, it says "Cannot find symbol Assertions". In a similar vein, I have:
#org.junit.jupiter.api.Test
before each test method and when I hover, I get "Cannot resolve symbol Test"
I simply want to create and then run some unit tests but obviously am doing something wrong.
Any ideas?
Thanks!

Gradle
Add the following dependency to your Gradle:
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")
Under your dependencies.
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.1")

I don't know you are using or maven or gradle. But if you are using maven just add below dependecies in between your tags. Let me know if you need more help regarding this. I have used older version below, you can check the latest version from https://mvnrepository.com and update the pom script.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>

If you use IntelliJ-IDEA, make sure your test file is in the Test Sources roots.
Because my project do not have the path src/test/java, when I use Ctrl+Shift+T to add a test file, it added in src/main/java...
See intellij support post

Maven
If using Maven, be sure to specify a dependency element inside the dependencies element.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
<scope>test</scope>
</dependency>
If you want to use those Assertions outside of your test-related classes, in your regular app classes, drop the <scope>test</scope> element.
Note that as of 5.4.0 of JUnit, we can specify the new single Maven artifact of junitjupiter which in turn will supply 8 libraries to your project. Very convenient if you are writing only JUnit 5 tests (Jupiter test engine), and not “vintage” JUnit 4 tests or other test engines.
If using Gradle rather than Maven, see the Answer by RileyManda.

Related

JUnit dependency not available in Maven project

I have already rebuilt the Maven repository, but my Maven still does not see JUnit:
I use Eclipse IDE,
Tried reinstalling the library also manually installed maven,
Replaced part of the code in pom, does anyone know how to solve the problem
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
The log says Compiling 2 source files to C:\workspace\Reverse2\target\classes instead of ...target\test-classes which indicates that src/test is handled as main code, not as test code. In main code, dependencies with the scope test are not available, hence the compile error.
In Maven test code is by default in src/test/java, not in src/test.

Getting "no groovy library is defined for module" on a maven project when compiling the project from Intellij

Below is the pom.xml content. I generated the project with maven archetype quickstart with junit. I encounter two problems:
When trying to run tests from Intellij, I get "no Groovy library defined", although I have nothing to do with Groovy in this project.
When doing mvn test from command line, no tests are identified.
To mention, the default java version is 8 on my mac, so when running mvn i export JAVA_HOME to 12, maybe it's related to the problem nr.1, not sure.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.pcollections</groupId>
<artifactId>pcollections</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
When trying to run tests from Intellij, I get "no Groovy library defined", although I have nothing to do with Groovy in this project.
This sound like a bug, since both pcollections and junit are not related to groovy:
Try to re-import your project (maven tab + reimport). If this doesn't help, close the project, and open up again the pom.xml it will offer to re-create the project and discard old data, agree to this and it will rebuild everything.
When doing mvn test from command line, no tests are identified.
Probably the tests that you've developed do not follow the surefire convention for classes resolution (It should end with *Test although if I remember correctly it has changed slightly in the latest versions of surefire, so now, also *Tests is ok). Please add the maven command line output when it runs the "test" phase as well as one of your test definitions (even with a trivial test code).

Cannot resolve symbol 'Assertions' <-- Error message when trying to use AssertJ in IntelliJ

Similar to some other Questions, I find IntelliJ mysteriously refuses to recognize AssertJ library. I am asking again as (a) I have tried the various suggestions, and (b) I have a very simple example anyone can try themselves.
In IntelliJ 2018 and IntelliJ 2019 pre-release, I create a new project using the Maven archetype maven-archetype-quickstart version 1.4.
AssertJ 3 requires Java 8. So I changed these two lines in the POM for 1.7 to 11.
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
I add this to the POM:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
Using the Maven panel in IntelliJ, I executed a clean and install.
Seems good. I verify the org.assertj:assertj-core:3.11.1 library appears in the Project panel of IntelliJ. The app runs, with Hello World appearing on the console in IntelliJ.
In the App.java file, I add this import statement.
import static org.assertj.core.api.Assertions.* ;
Error reported in the IDE editor:
Cannot resolve symbol 'Assertions'
Some people suggest a corrupted Maven cache. So I quit IntelliJ, and I delete the .m2 folder in my home folder. I re-open my project in IntelliJ, and re-execute the Maven clean & install. Many things are downloading, so I know the Maven cache is indeed being recreated.
Yet, still the error in my editor, Cannot resolve symbol 'Assertions'.
No Java Modules involved, as the quickstart archetype has not yet been updated for that.
Delete <scope>test</scope>
This topic was addressed in a closed ticket # 520 on the AssertJ issue tracker.
When a Maven dependency carries a scope element with a value of test, that means you cannot use that library outside of your test-specific source package/folder.
If you are trying to call AssertJ from code in your example project’s src/main/java/… folder hierarchy, you will see that error. If you call AssertJ from src/test/java…, you will see success.
To enable AssertJ in the src/main/java/… folder hierarchy, delete the scope element in your POM dependency. So this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
<scope>test</scope>
</dependency>
…becomes this:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.11.1</version>
</dependency>

Cannot use JUnitCore

I'm pretty new to java and unit testing and I'm experimenting with junit and so I watch tutorials and read blogs about it. From time to time there are things like PrimerClasses and TestSuites and everyone seems to have no problem using these kind of functionalities.
So when I try to use these too, my project cannot find JUnitCore or even the runner, where all that pretty stuff comes from and up to now I was not able to work out what the reason might be.
Have you added dependency of JUnit in your application?
If you are unable to find the classes then you should add proper dependencies.
If you have created the maven project, then add below lines in your pom file.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Or download the jar and add it to you project classpath.

How do I get depend on all artifacts from a group of a version from maven?

I'm new to Maven and I'm trying to build a project for the first time. I want to write some code that depends on apache lucene. Here's a list of artifacts in maven that I'm trying to get.
Is there any way instead of explicitly listing each artifact, I could simply depend on all artifacts of a given version? I tried this:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>*</artifactId>
<version>3.6.1</version>
</dependency>
which gave me the error
'dependencies.dependency.artifactId' for org.apache.lucene::jar with value '' does not match a valid id pattern. # line 19, column 19
I can verify that I can download dependencies when I explicitly state them. IE this works fine:
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.1</version>
</dependency>
I realize depending on everything in lucene is probably sub-optimal, but for doing something quick-and-dirty I'd hate to have to manually populate all these little lucene libraries. What is the typical practice for getting a large set of related dependencies in maven?
Short answer: you can't. Remember you just do this once and later you can simply copy-paste dependencies (not very DRY though). Also consider creating an archetype that will quickly create a skeleton with all required dependencies (for quick and dirty projects).
Longer answer: well, you can work around that. Create a separate pom.xml with:
<packaging>pom</packaging>
and declare all Lucene dependencies there manually, one after another. Once and for all. Later you can simply add a dependency to your pom.xml (that is to groupId/artifactId/version defined there) which will transitively include all dependencies of that pom.xml.
Talking about transitivity: if you depend on a JAR in maven and that JAR has other dependencies, you get that transitive dependencies implicitly. Examine Lucene poms, maybe it's enough to import few of them and rely on transitive dependencies?
Inside a single dependency for a groupId add different artifactId's
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<artifactId>spring-context</artifactId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>

Categories