Skip to content

Instantly share code, notes, and snippets.

@wkorando
Created September 8, 2018 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkorando/fd10183398e5e4700086f92a804eba28 to your computer and use it in GitHub Desktop.
Save wkorando/fd10183398e5e4700086f92a804eba28 to your computer and use it in GitHub Desktop.
@ExtendWith(MockitoExtension.class)
public class TestMockitoInjection {
private BoringService service;
public TestMockitoInjection(@Mock BoringService service) {
this.service = service;
}
@Test
public void testConstructorInjectedValue() {
when(service.returnNumber()).thenReturn(2);
assertEquals(2, service.returnNumber());
}
@Test
public void testMethodInjection(@Mock BoringService service) {
when(service.returnNumber()).thenReturn(3);
assertEquals(3, service.returnNumber());
}
public class BoringService {
public int returnNumber() {
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment