Spring Boot tutorial 14--integration test

Source: Internet
Author: User
Tags assert spring boot tutorial

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&GT; 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.