The SpringRunner
provides support for loading a Spring ApplicationContext
and having beans @Autowired
into your test instance. It actually does a whole lot more than that (covered in the Spring Reference Manual), but that's the basic idea.
Whereas, the MockitoJUnitRunner
provides support for creating mocks and spies with Mockito.
However, with JUnit 4, you can only use one Runner
at a time.
Thus, if you want to use support from Spring and Mockito simultaneously, you can only pick one of those runners.
But you're in luck since both Spring and Mockito provide rules in addition to runners.
For example, you can use the Spring runner with the Mockito rule as follows.
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTests {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
MyService myService;
// ...
}
Though, typically, if you're using Spring Boot and need to mock a bean from the Spring ApplicationContext
you would then use Spring Boot's @MockBean
support instead of simply @Mock
.
When SpringRunner.class
is used, Spring provides corresponding annotations:
@MockBean
@SpyBean
Mocks are injected to objects under tests via @Autowired
annotation. To enable this functionality tests must be annotated with
@SpringBootTest
or
@TestExecutionListeners(MockitoTestExecutionListener.class)
More details and examples can be found in the official documentation: Mocking and Spying Beans
As per the JavaDoc:
SpringRunner is an alias for the
SpringJUnit4ClassRunner
. To use this class, simply annotate a JUnit 4 based test class with@RunWith(SpringRunner.class)
. If you would like to use the SpringTestContext
Framework with a runner other than this one, useorg.springframework.test.context.junit4.rules.SpringClassRule
andorg.springframework.test.context.junit4.rules.SpringMethodRule
.
And the JavaDoc of TestContext
:
TestContext
encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.
That of method getApplicationContext()
:
Get the application context for this test context, possibly cached. Implementations of this method are responsible for loading the application context if the corresponding context has not already been loaded, potentially caching the context as well.
So, SpringRunner does load the context and is responsible for maintaining it. For example, if you want to persist data into an embedded database, like H2 in-memory database, you have to use SpringRunner.class
; and, to clean the tables to get rid of the records you inserted after every test, you annotate the test with @DirtiesContext
to tell Spring to clean it.
But, this is already an integration or component test. If your test is pure unit test, you don't have to load DB, or you just want to verify some method of a dependency is called, MockitoJUnit4Runner
suffices. You just use @Mock
as you like and Mockito.verify(...)
and the test will pass. And it is a lot quicker.
Test should be fast. As fast as possible. So whenever possible, use MockitoJUnit4Runner
to speed it up.
@Service
public class MySuperSpringService {
@Autowired
private MyRepository innerComponent1;
@Autowired
private MyExternalApiRest innerComponent2;
public SomeResponse doSomething(){}
}
So, in order to test this service MySuperSpringService which needs another spring beans: MyJpaRepository and MyExternalApiRest, you need to mock them using @MockBean and create the result as you need.
import static org.mockito.Mockito.when;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import junit.framework.TestCase;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = your.package.Application.class)
public class MySuperSpringServiceTest extends TestCase {
@Autowired
private MySuperSpringService serviceToTest;
@MockBean
private MyRepository myRepository;
@MockBean
private MyExternalApiRest myExternalApiRest;
@Before
public void setUp() {
Object myRepositoryResult = new Object();
//populate myRepositoryResult as you need
when(myRepository.findByClientId("test.apps.googleusercontent.com"))
.thenReturn(myRepositoryResult);
Object myExternalApiRestResult = new Object();
//populate myExternalApiRestResult as you need
when(myExternalApiRest.listUserRoles("[email protected]")).thenReturn(myExternalApiRestResult);
}
@Test
public void testGenerateTokenByGrantTypeNoDatabaseNoGoogleNoSecurityV1(){
SomeResponse response = serviceToTest.doSomething();
//put your asserts here
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With