0

I have some test classes by Mockito . I try to aggregate them by TestSuite but it has error .

First Test Case :

@RunWith(MockitoJUnitRunner.class)
public class GetLastLoginTimeTest {

    @Test
    public void happyScenario_OK() throws Exception{
        ....
    }
    ....
}

Second Test Case :

@RunWith(MockitoJUnitRunner.class)
public class FetchUserLoginHistoryTest {

    @Test
    public void happyScenario_OK() throws Exception{
        ....
    }
    ....
}

Suite Test Class :

@RunWith(MockitoJUnitRunner.class)
@SuiteClasses({GetLastLoginTimeTest.class , FetchUserLoginHistoryTest.class})
public class LoginHistoryBLTestSuite {

error :

org.mockito.exceptions.base.MockitoException: 

No tests found in LoginHistoryBLTestSuite
Is the method annotated with @Test?
Is the method public?

    at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:75)
    at org.mockito.internal.runners.RunnerFactory.createStrict(RunnerFactory.java:40)
    at org.mockito.junit.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:154)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at ....

If I change

@RunWith(MockitoJUnitRunner.class)

to

@RunWith(Suite.class)

then mockito expectations fail by NullPointerException .

Amin Arab
  • 530
  • 4
  • 19
  • We should not use `@RunWith(MockitoJUnitRunner.class)` for classes which represents Suite. Now, this works for me if we run it with `@RunWith(Suite.class)`. Could you share code and NPE stack trace? – Anshul Singhal May 31 '19 at 03:55

1 Answers1

0

Like Anshul said in the comment above: You are not supposed to run your testSuite with MockitoJunitRunner.

You can use RunWith(MockitoJUnitRunner.class) annotation on the test cases or even better just use a small amount of static imports you need for your testing.

An example would be:

import static org.mockito.Mockito.verify;
(...)

public class ClassUnderTestTest {
  @Rule
  public MockitoRule mockitoRule = MockitoJUnit.rule();

  @Mock
  private ClassToMock mockClassInstance;

  @InjectMocks
  ClassUnderTest classUnderTestInstance;

  @Test
  public void testMethodFunctionality() throws Exception {
    classUnderTestInstance.callFunctionInMock()

    verify(mockClassInstance).calledFunction();
  }

And the AllTests suite would look like

@RunWith(Suite.class)
@SuiteClasses({ClassUnderTestTest.class})
public class AllTests {

}
Magelan
  • 189
  • 1
  • 13