Right Click on Project > Properties > Java Build Path > Add the Test folder as source folder.
All source folders including Test Classes need to be in Eclipse Java Build Path. So that the sources such as main and test classes can be compiled into the build directory (Eclipse default folder is bin).
Answer from iaacp on Stack OverflowRight Click on Project > Properties > Java Build Path > Add the Test folder as source folder.
All source folders including Test Classes need to be in Eclipse Java Build Path. So that the sources such as main and test classes can be compiled into the build directory (Eclipse default folder is bin).
If none of the other answers work for you, here's what worked for me.
Restart eclipse
I had source folder configured correctly, and unit tests correctly annotated but was still getting "No JUnit tests found", for one project. After a restart it worked. I was using STS 3.6.2 based of eclipse Luna 4.4.1
java - No tests found with test runner 'JUnit 4' - Stack Overflow
Running Junit Test on a Single File[Resolved] - Quality Assurance - OpenMRS Talk
java - Maven not running JUnit 5 tests - Stack Overflow
java - Junit method not found - Stack Overflow
In your test, you are defining InMemory as a mock. A mock is an object that emulates the behavior of a real object according to how you configure such mock to behave. Having said that, your InMemory object in your test is not a real InMemory object, but a simple mock which means that its real code will not be called. Take a look at @Spy and consider replacing @Mock with it. Find more information in the following articles:
- https://medium.com/swlh/what-is-the-difference-between-a-mock-and-a-stub-bd6b639e9fa5
- https://www.baeldung.com/mockito-annotations#mock-annotation
in first step we must replace @Mock to Spy
and then we must change in when() method to when(spyInstance.method) and then we need called method has spyInstance.method to testing
this is my code after fixed you can see it
@Spy
InMemory inMemoryGet; // replace it from mock to spy
@InjectMocks
UserService userService;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void registry(){
UserDto emp = new UserDto(1L,"Lokesh","Gupta",25); // add it
when(inMemoryGet.save(emp.ToDTO(emp))).thenReturn(emp.ToDTO(emp));
verify(inMemoryGet, times(0)).save(emp.ToDTO(emp)); // how many time invoke
userService.registry(emp); // called method from here
verify(inMemoryGet, times(1)).save(emp.ToDTO(emp));
System.out.println("\"Test\" = " + "Pass");
}
this just happened to me. Rebuilding or restarting Eclipse didn't help.
I solved it by renaming one of the test methods to start with "test..." (JUnit3 style) and then all tests are found. I renamed it back to what it was previously, and it still works.
When we get these errors it seems like Eclipse is just confused. Restart Eclipse, refresh the project, clean it, let Eclipse rebuild it, and try again. Most times that works like a charm.
According to the annotation (import org.junit.jupiter.api.Test), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
Your version of Maven comes with a version of maven-surefire-plugin which does not support JUnit 5. You could update your Maven to the latest version. You could also set the version of the maven-surefire-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.0</version>
</plugin>
See the junit5-samples for this information.
See the Maven Surefire Plugin artifact in a Maven repository. At version 3.0.0-M3 as of 2019-01.
tl;dr
Add two dependencies to your project:
- JUnit Jupiter (Aggregator)
- Maven Surefire Plugin
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
… and:
<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<type>maven-plugin</type>
</dependency>
junit-jupiter — the simpler archetype for JUnit 5
The Answer by LaurentG seems to be correct, but a bit outdated.
As of JUnit 5.4, you can replace those multiple Maven artifacts:
junitjunit-jupiter-apijunit-jupiter-engine
…with a single artifact:
junit-jupiter
…to run JUnit 5 tests.
This artifact is an aggregate of other artifacts, a convenient wrapper to simplify your POM file. See this repository.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
This gives you all you need to write and run JUnit 5 Jupiter tests.
maven-surefire-plugin
You would also need the Surefire plugin as shown in that other Answer. Be sure to get the latest version, as Surefire has had some important fixes/enhancements recently.
See this list of dependency declarations for Maven, Buildr, Ivy, Grape, Grails, SBT, and Leiningen. To quote the Maven config:
<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<type>maven-plugin</type>
</dependency>
junit-vintage-engine for JUnit 3 & 4 tests
If you have old JUnit 3 or JUnit 4 legacy tests that you want to continue to run, add another dependency, junit-vintage-engine.
<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
You've imported the types, but not used a static import to make the members available without qualification. If you use:
import static org.junit.Assert.*;
then that should statically import all the static methods in the Assert class, so you can write assertTrue instead of Assert.assertTrue.
Note that presumably Assert itself has nested types, otherwise I'd have expected your "normal" import to fail.
You have to do a static import.
import static org.junit.Assert.*;
