Integration testing typically requires the interaction of different objects from different layers, such as databases, network connections, IOC containers, and so on.
Spring provides top-level support for integration testing through the Spring TestContext framework. It does not depend on a specific test framework, it can be used with junit, or it can use testng.
MAVEN-built projects default to the catalog of tests: Src/test/java (test code), Src/test/resources (test Resource), and Src/main/java (project source), Src/main/resources ( Project resources).
Spring provides a Springjunit4classrunner class that provides the functionality of the Spring TestContext framework. Configure application Context through @contextconfiguration to determine the profile of the activity through @activeprofiles.
After using the spring test, the "Run" section of our previous example can use the spring test to verify that the functionality works.
Example
1> Increase the dependency of the spring test in Pom.xml
<!--Spring Test Support - <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-test</Artifactid> <version>${spring-framework.version}</version> </Dependency> <Dependency> <groupId>Junit</groupId> <Artifactid>Junit</Artifactid> <version>4.11</version> </Dependency>
2> business code (SRC/MAIN/JAVA)
Packagecom.wisely.highlight_spring4.ch3.fortest; Public classTestbean {PrivateString content; PublicTestbean (String content) {Super(); This. Content =content; } PublicString getcontent () {returncontent; } Public voidsetcontent (String content) { This. Content =content; } }
3>. Configuration Class (Src/main/java)
Packagecom.wisely.highlight_spring4.ch3.fortest;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.context.annotation.Profile; @Configuration Public classtestconfig {@Bean @Profile ("Dev") PublicTestbean Devtestbean () {return NewTestbean ("From Development profiles"); } @Bean @Profile ("Prod") PublicTestbean Prodtestbean () {return NewTestbean ("From Production profiles"); }}
4> test (Src/test/java)
Packagecom.wisely.highlight_spring4.ch3.fortest;ImportOrg.junit.Assert;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.test.context.ActiveProfiles;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; the @RunWith (Springjunit4classrunner.class)//Springjunit4classrunner provides the functionality of the Spring TestContext framework in a junit environment@ContextConfiguration (classes = {Testconfig.class})//@ContextConfiguration used to load the configuration ApplicationContext, where the classes property is used to load the configuration class@ActiveProfiles ("prod")//@ActiveProfiles used to declare the profile of an activity Public classdemobeanintegrationtests {@Autowired//you can inject beans using normal @autowired. PrivateTestbean Testbean; @Test//test the code with JUnit assert to verify that the results are consistent with expectations Public voidProdbeanshouldinject () {String expected= "From production profile"; String actual=testbean.getcontent (); Assert.assertequals (expected, actual); } }
Spring Boot tutorial 14--integration test