Spring boot 1.4.2 Unit Test Configuration

Source: Internet
Author: User

The older version of the new edition simplifies a lot

Add directly on the test class

@RunWith (Springrunner.  class = Own startup class, Webenvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

Colleague injects the Testresttemplate class and injects the current port number through @LocalServerPort annotations

The specific test code is as follows:

@RunWith (Springrunner.class) @SpringBootTest (Classes= Serverapplication.class, webenvironment =SpringBootTest.WebEnvironment.RANDOM_PORT) Public classTest {@LocalServerPortPrivate intPort; PrivateURL Base; PrivateGson Gson =NewGson (); @AutowiredPrivatetestresttemplate resttemplate; @Before Public voidSetUp ()throwsException { This. Base =NewURL ("http://localhost:" + port + "/"); } @Test Public voidTest () {responseentity<String> test = This. Resttemplate.getforentity ( This. base.tostring () + "/test", String.class, "Test");    System.out.println (Test.getbody ()); }}

Controller

@RestController @requestmapping ("/test")  Public class Processinstancecontroller {    = "", method = requestmethod.get)    public  String Test () {        return "Greetings from Spring boot!" ;    }}
Serverapplication:
@SpringBootApplication Public classServerapplicationextendsSpringbootservletinitializerImplementsCommandlinerunner {@OverrideprotectedSpringapplicationbuilder Configure (Springapplicationbuilder application) {returnApplication.sources (serverapplication.class); }     Public Static voidMain (string[] args)throwsIOException {springapplication.run (serverapplication.class, args); } @Override Public voidRun (String ... strings)throwsException {System.out.println ("Initialize ......" "); }}

Spring boot 1.4.2 Unit Test Configuration

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.