30

I am getting an error like this when running my test:

org.mockito.exceptions.base.MockitoException: 

No tests found in TestCase
Haven't you forgot @Test annotation?

I certainly do have a method annotated with @Test. What am I doing wrong?

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87

7 Answers7

64

I got this exception even though I had a public method annotaded with @Test. Turned out was importing org.junit.jupiter.api.Test, I changed to org.junit.Test and it worked fine.

Jefferson Lima
  • 5,186
  • 2
  • 28
  • 28
  • 2
    This means you have both the Junit 4.x and Junit 5.x libraries on your classpath but you likely don't have all the required dependencies for 5.x, or whatever runner you are using isn't updated to be able to run 5.x. The junit 5.x has the capability to run 4.x tests – Darren Forsythe Jan 24 '20 at 14:01
  • 2
    Saved a lot of time! – Abhishek Deshmukh Jan 12 '21 at 20:38
54

The method needs to be explicitly declared as public:

@Test
public void asdf() {
    asdf...
}
Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
9

Import junit package in stead of testng package.

Bingo
  • 269
  • 3
  • 7
  • 2
    Bingo! @Test annotation was from TestNG testing library indeed. Changing to JUnit in imports helped. – Zon Mar 31 '18 at 17:47
7

If you've upgraded to Junit 5, according to this How to use Mockito with JUnit5:

"No Rules, No Runners JUnit 4 rules and runners don't work in JUnit 5, so the MockitoRule and the Mockito runner can not be used."

Use @ExtendWith(MockitoExtension.class) instead. Verified this on Junit 5.6.2 and Spring boot 2.3.1:

@ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) public class ClassTest {}

Don't also forget to explicitly declare the test method as public as well.

Praz01
  • 71
  • 1
  • 3
1

Just remove the public access modifier from the test class and go with the

import org.junit.jupiter.api.Test;
1

you need to check the import of the test in the class for example from :import org.junit.jupiter.api.Test; into: import org.junit.Test;

m Piroli
  • 333
  • 3
  • 6
0

Class has be public test method has be public