57

I was using @RunWith(MockitoJUnitRunner.class) for my junit test with mockito. But now i am working with spring boot app and trying to use @RunWith(SpringRunner.class) . Does using @RunWith(SpringRunner.class) has any advantages over using @RunWith(MockitoJUnitRunner.class)? Can i still use feature like @Injectmock, @Mock, @Spy with @RunWith(SpringRunner.class)

5 Answers5

56

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.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • How to replace @RunWith(SpringRunner.class) with a Rule like you did for MockitoJUnit ? – GOXR3PLUS Nov 20 '18 at 08:53
  • 3
    Using the `SpringClassRule` and `SpringMethodRule`: https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/testing.html#testcontext-junit4-rules – Sam Brannen Nov 20 '18 at 12:41
  • One more think :) Can i combine it with PowerMock Rules or run with PowerMock synchroniously :) ? – GOXR3PLUS Nov 20 '18 at 13:47
  • 1
    I don't use PowerMock, so I unfortunately cannot answer that question. – Sam Brannen Nov 21 '18 at 13:29
  • For example, when testing Spring-Data interfaces I use `@MockBean` BUT when testing JPA query implementation classes I use `@Mock` – djangofan Mar 29 '21 at 21:50
15

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

VaL
  • 1,128
  • 15
  • 29
  • 1
    mocks cannot be injected with Autowired, you should use MockBean instead – voipp Nov 07 '18 at 08:33
  • 2
    @voipp mocks can be injected to tested objects using Autowired, but MockBean is used to define mocks to be injected – VaL Nov 07 '18 at 09:41
5

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 Spring TestContext Framework with a runner other than this one, use org.springframework.test.context.junit4.rules.SpringClassRule and org.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.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 1
    `SpringRunner` does not load any context it just enable spring boot features like `@Autowire`, `@MockBean`, etc.. during junit testing. If you want to load the context you'd rather use `@SpringBootTest` – Hamza Belmellouki Jun 02 '20 at 21:34
1

Scenario 2019 November : spring-boot : 2.1.1.RELEASE

  • You have a spring boot api rest
  • You need to test a service called MySuperSpringService
  • But, inside MySuperSpringService, are required two more autowires. One to perform a sql select (MyJpaRepository) and another to call an external api rest (MyExternalApiRest)
@Service
public class MySuperSpringService {

  @Autowired
  private MyRepository innerComponent1;

  @Autowired
  private MyExternalApiRest innerComponent2;

  public SomeResponse doSomething(){}
}

How test MySuperSpringService mocking database and external api rest ?

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("john@doe.com")).thenReturn(myExternalApiRestResult);

  }

  @Test
  public void testGenerateTokenByGrantTypeNoDatabaseNoGoogleNoSecurityV1(){
    SomeResponse response = serviceToTest.doSomething();
    //put your asserts here
  }

}
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • couple of questions ... is `@SpringBootTest` required and do we need `@MockBean` ? Can this be done simply using `@Mock` ? – satish marathe Feb 21 '20 at 00:09
  • @ SpringBootTest is super required for test!! @ Mock is for simple java classes. If the target class is an spring component with several @ Autowired dentro, @ MockBean is your only option! – JRichardsz Feb 21 '20 at 13:36
  • @JRichardsz I don't think so. You can mock the dependencies of the spring component. – Sudip Bolakhe Feb 26 '20 at 03:42
0

you can absolutely use SpringRunner for both unit tests and integration tests. SpringRunner Tutorial

Caffeine Coder
  • 948
  • 14
  • 17