Skip to content

Instantly share code, notes, and snippets.

@adashrod
Created November 27, 2019 05:34
Show Gist options
  • Select an option

  • Save adashrod/3fd01480a13ad62a0214f37ce7dd1935 to your computer and use it in GitHub Desktop.

Select an option

Save adashrod/3fd01480a13ad62a0214f37ce7dd1935 to your computer and use it in GitHub Desktop.
I ran into a weird problem with Mockito and JUnit 5. A test passed in IntelliJ, but failed at the command line.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
/**
* Last tested with Mockito 3.x and JUnit 5.4.x
* Weird behavior: the first test fails when run from CLI: ant, java -jar, but passes when run by IntelliJ
* The second call to Mockito.when throws NPE
*/
@ExtendWith(MockitoExtension.class)
public class MockitoJUnitIsBrokenTest {
public interface StringProvider {
String getString(String s);
}
/**
* This test only fails when the annotation `@ExtendWith(MockitoExtension.class)` is used on the class.
* The annotation is needed if using @Mock on fields, using Mockito.mock and removing the ExtendWith
* annotation fixes the problem. The alternative mocking strategy of the other two tests also fixes it.
*/
@Test
public void testWorksOnlyInIde() {
final StringProvider sp = Mockito.mock(StringProvider.class);
when(sp.getString(eq("one"))).thenReturn("ONE");
when(sp.getString(eq("two"))).thenReturn("TWO"); // throws NPE when run from CLI, but works in IntelliJ o_O
final String f = sp.getString("one");
final String s = sp.getString("two");
Assertions.assertEquals("ONE", f);
Assertions.assertEquals("TWO", s);
}
@Test
public void testWorksInIdeAndCli() {
final StringProvider sp = Mockito.mock(StringProvider.class);
when(sp.getString(anyString())).thenAnswer(invocationOnMock -> {
final String a = invocationOnMock.getArgument(0);
if (a.equals("one")) {
return "ONE";
} else if (a.equals("two")) {
return "TWO";
}
return null;
});
final String f = sp.getString("one");
final String s = sp.getString("two");
Assertions.assertEquals("ONE", f);
Assertions.assertEquals("TWO", s);
}
@Test
public void testAlsoWorksInBoth() {
final StringProvider sp = Mockito.mock(StringProvider.class);
doReturn("ONE").when(sp).getString(eq("one"));
doReturn("TWO").when(sp).getString(eq("two"));
final String f = sp.getString("one");
final String s = sp.getString("two");
Assertions.assertEquals("ONE", f);
Assertions.assertEquals("TWO", s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment