@RunWith(SpringRunner.class) is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in out JUnit tests, this annotation will be required.
Difference:
- @ContextConfiguration is an annotation from the Spring Test Framework, which is suitable for every Spring application,
- (removed) @SpringApplicationConfiguration is from Spring Boot and is actually a composite annotation, which includes ContextConfiguration with the custom SpringApplicationContextLoader as loader.
Configuration db
- create database see
src/main/resources/db.sql
cp src/main/resources/application.properties.dist src/main/resources/application.properties
cp src/test/resources/application.properties.dist src/test/resources/application.properties
- set db config in application.properties
see Test:
/src/test/java/bare_junit/EmployeeTest.java
!Important, when use SpringRunner.class - spring context will be loaded
see Test:
/src/test/java/unit/mockito_spring_runner/MyServiceTest.java
@Mock
private Data mockData;
@InjectMocks
private MyService service;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getMainData() {
when(mockData.getInfo()).thenReturn("ahaha");
String info = service.getMainData();
assertThat(info, is("ahaha"));
}
but if in your service there are @Autowired classes that you do not want to get wet - then you need to remember to add @Autowired for @InjectMocks.
See example: /src/test/java/unit/mockito_spring_runner/MyServiceWithTwoDepsTest.java
When use class ConcatStr for non-mocking
@Mock
private Data mockData;
@Autowired
@InjectMocks
private MyServiceWithTwoDeps service;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getMainData() {
when(mockData.getInfo()).thenReturn("ahaha");
String info = service.getMainData();
assertThat(info, is("ahaha"));
}
!Important, when use MockitoJUnitRunner.class - spring context will not be loaded
see Test:
/src/test/java/unit.mockito_junit_runner/MyServiceTest.java
see Test:
/src/test/java/unit/mockito_bean_with_scan/MyServiceTest.java
see Test:
/src/test/java/unit/mockito_bean_without_scan/MyServiceTest.java
For create test with spring features:
@RunWith(SpringRunner.class)
is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in out JUnit tests, this annotation will be required.- Add
ConfigClass
- where configuration @Bean (or use main Application for scan all package)
Example ConfigClass
:
@Configuration
@ComponentScan("hello.simplelogic")
public class MyServiceTestConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@ContextConfiguration(classes = Application.class)
see /src/test/java/integration/with_config_main_package/EmployeeDaoImplTest.java
see /src/test/java/integration.with_config_local/MyServiceTest.java
see /src/test/java/integration.with_config_main_package/EmployeeDaoImplTest.java
So as not to create different configuration files for all tests - you can create common for many tests.
Or create an abstract test class with a configuration class
see /src/test/java/integration.spring_integration_with_common_config/MyServiceTest.java