Unit tests using @springboottest annotations

Source: Internet
Author: User
Overview

The @SpringBootTest annotation is an annotation for testing that was introduced by Springboot since the 1.4.0 version. The basic usage is as follows: 1. Add maven dependencies

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </propert ies> <parent> <groupId>org.springframework.boot</groupId> <artifactid>spri ng-boot-starter-parent</artifactid> <version>1.5.6.RELEASE</version> </parent> &L
            T;dependencies> <dependency> <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPRING-BOOT-STARTER-TEST&L
            t;/artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid >spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> 

2. Write Startup entry class
@SpringBootApplication public
class Startupapplication {public
    static void Main (string[] args) {
        Springapplication.run (Startupapplication.class, args);
    }

3. Writing the Controller class
@RestController public
class Hellocontroller {

    @RequestMapping ("/") public
    String Index () {
        Return "Hello Spring boot,index!";
    }

    @RequestMapping (value = "/test", method = requestmethod.get) public
    String test () {return
        "Spring Boot test Dem O! ";
    }
}

4. Writing test class
 @RunWith (Springrunner.class) @SpringBootTest (classes = startupapplication.class, webenvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class Hellocontrollertest {/** * @LocalServerPort provides @Value (

    "${local.server.port}") instead of/@LocalServerPort private int port;

    Private URL base;

    @Autowired private Testresttemplate resttemplate;
        @Before public void SetUp () throws Exception {String url = String.Format ("http://localhost:%d/", port);
        System.out.println (String.Format ("Port is: [%d]", port));
    this.base = new URL (URL); /** * Sends the request to the "/test" address and prints the result * @throws Exception * * @Test public void test1 () throws Excepti  on {responseentity<string> response = this.restTemplate.getForEntity (This.base.toString ()
        + "/test", String.class, "" ");
    System.out.println (String.Format ("Test Result:%s", Response.getbody ())); }

where the Classes property specifies the startup class, SpringBootTest.WebEnvironment.RANDOM_PORT is often used with the @localserverport in the test class to inject the property. Randomly generates a port number. Summary

We found that as the spring boot version was upgraded, unit testing became simpler.

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.