Springboot's Create Your simple application&tests__springboot

Source: Internet
Author: User

Application.java

Package Com.zhu;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication public
class DemoApplication {public

    static void Main (string[] args) {
        Springapplication.run (Demoapplication.class, args);
    }

@Bean public
    Commandlinerunner Commandlinerunner (ApplicationContext ctx) {return
        args-> {

            SYSTEM.OUT.PRINTLN ("Let's inspect the beans provided by Spring Boot:");

            string[] Beannames = Ctx.getbeandefinitionnames ();
            Arrays.sort (beannames);
            for (String beanname:beannames) {
                System.out.println (beanname);}}

        ;
    }

the website's description of @springbootapplication

Here's what you need to say:

Springboot has now been updated to 1.5.1, and there are differences in the way Application.java annotations are made in 1.4.4;


1.4.4 uses three annotations, using @springbootapplication on the official web site to replace the above three annotations by default. We give priority to the latest

Then the controller file:

Package Com.zhu.controller;

Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;

@RestController public
class Hellocontroller {
    @RequestMapping ("/") public
    String Index () {return
        " Greetings from Spring boot! ";
    }
}"

The use of SPRINGMVC should be clear, the way back to the view annotation is @controller, return the data annotation plus @responsebody, but in springboot use @restcontroller combined the two annotations, The data is returned instead of a view.
The official website wrote:

Enter Application.java-–run application
Browser input URL, access

Add Unit Tests
adding dependencies in Pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId> spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>

Path: Src\test\java\com\zhu\hellocontrollertest.java

Package Hello;
Import static org.hamcrest.Matchers.equalTo;
Import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

Import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import ORG.SPRINGFRAMEWORK.BOOT.TEST.AUTOCONFIGURE.WEB.SERVLET.AUTOCONFIGUREMOCKMVC;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.http.MediaType;
Import Org.springframework.test.context.junit4.SpringRunner;
Import ORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC;

Import Org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    @RunWith (springrunner.class) @SpringBootTest @AutoConfigureMockMvc public class Hellocontrollertest {@Autowired

    Private MOCKMVC MVC; @Test public void Gethello () throws Exception {Mvc.perform (Mockmvcrequestbuilders.get ("/"). Accept (MedIatype.application_json)). Andexpect (Status (). IsOk ()). Andexpect (Content (). String (Equalto (
    "Greetings from Spring boot!")); }
}


This mockmvc comes from springtest, compiles the HTTP request through the compiler, and asserts the result. Use @autoconfiguremockmvc and @springboottest to inject the MOCKMVC instance.

You can also use the full stack integration test.

Package Hello;
Import static org.hamcrest.Matchers.equalTo;

Import static org.junit.Assert.assertThat;

Import Java.net.URL;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.boot.context.embedded.LocalServerPort;
Import Org.springframework.boot.test.context.SpringBootTest;
Import Org.springframework.boot.test.web.client.TestRestTemplate;
Import org.springframework.http.ResponseEntity;

Import Org.springframework.test.context.junit4.SpringRunner; @RunWith (Springrunner.class) @SpringBootTest (webenvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public

    Class Hellocontrollerit {@LocalServerPort private int port;

    Private URL base;

    @Autowired private testresttemplate template;
    @Before public void SetUp () throws Exception {this.base = new URL ("http://localhost:" + port + "/"); @Test public void Gethello () thrOWS Exception {responseentity<string> response = template.getforentity (Base.tostring (), STR
        Ing.class);
    Assertthat (Response.getbody (), Equalto ("Greetings from Spring boot!")); }
}


The implanted service starts with a random port. After JUnit you will find that the port number is inconsistent with the default ports you set.

congratulations! Built a simple Web application with Spring Boot and learned how it can ramp up your development pace

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.