I need to test old Spring Boot Java application (version of Spring Boot 1.2.7, Java 1.8) and I don’t want to change Spring version (because the application is very voluminous and then I will have to refactor the code very much).
I’d like to apply to input of my desktop application different table column types and test that application determines them correctly.
But the problem is that due to old version of Spring I can’t pull up annotations like @ExtendWith(SpringExtension.class), @ExtendWith(MockitoExtension.class), or @RunWith(SpringRunner.class) to enable spring boot features, or @SpringBootTest to load complete application context for end-to-end integration testing.
I’m using the following dependencies for tests:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.3.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
I tried to upgrade the version of Spring dependencies for testing (to be able to include annotations) and keep the Spring version of existing project but then initialization errors occured.
How can I enable spring boot features for testing?
Advertisement
Answer
The most recommended solution would be to upgrade the entire project. If not, the easiest solution is to rely on spring-boot-starter-test
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
This means you’ll have to rely on JUnit 4 and older versions of Mockito, AssertJ and the Spring testing library.
These are the annotations you can use:
@ExtendWith(SpringExtension.class)
/@RunWith(SpringRunner.class)
→@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(MockitoExtension.class)
→@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
→@SpringApplicationConfiguration
(not an exact replacement)
Test slices (@DataJpaTest
, @WebMvcTest
) were introduced in Spring boot 1.4, so you can’t rely on those either.
The best way to find out what is possible, is by checking the reference documentation of Spring boot 1.2.7. One example they show is:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class CityRepositoryIntegrationTests { @Autowired CityRepository repository; // ... }