SpringBoot

Unit testing

As we all know, programmers in the development process, every time after the completion of the development of a functional interface or business method of the code, usually will use the unit test to verify the development of the function to know whether the normal operation. So SpringBoot provides annotations and tools to help programmers test functionality. For use, you need to add the spring-boot-starter-test test dependency to the project Maven’s POm. XML file in advance, so that unit tests can be implemented through relevant annotations.

Add the spring-boot-starter-test dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
Copy the code

Write unit test classes to test

/ * * * SpringJUnit4ClassRunner. Class: Spring running environment * takeup in class: JUnit running environment * SpringRunner class: Spring * / Boot operation environment
@RunWith(SpringRunner.class)
// mark the current class as SpringBoot test class and load the project's ApplicationContext
@SpringBootTest
class SpringbootDemoApplicationTests {

    @Test
    void contextLoads(a) {}@Resource
    private DemoController demoController;


    @Test
    public void testDemoController(a){ String s = demoController.helloBoot(); System.out.println(s); }}Copy the code

In this code, the DemoController instance is injected with the @Resource annotation and contextLoads() is called in the testDemoController() method to print the result

SpringBoot hot deployment

Usually, in the process of development, programmers need to constantly change a business code test, to complete functions, but after modifying code you need to restart the service, and some service startup it takes a very long time, the modified restart operation greatly reduces the efficiency of application development. Therefore, SpringBoot specifically provides a dependent startup period for hot deployment that allows hot deployment of a project without the need to manually restart the project.

Hot deployment: After code changes are made, updates can be made without restarting the container

This section describes how to use hot deployment

  • Add the spring-boot-devtools dependency
<! Introducing hot deployment dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
Copy the code

SpringBoot hot deployment uses the IDEA development tool. Adding hot deployment dependencies has no effect. Therefore, you need to configure hot deployment functions for the IDEA development tool

  • IDEA Tool hot deployment Settings

Select the [File] -> [Settings] option of IDEA tool interface to open the Compiler panel setting page

Select the Compiler option under Build and select the “Build Project Automatically” option on the right to set the project to be automatically translated. Click Apply → OK to save the Settings

Use the combined shortcut “Ctrl+Shift+Alt+/” on any page of the project to open the Maintenance TAB, select and open the Registry page

Found in list “. The compiler. Automake. Allow the when. App. Running “, will check the Value of the Value, after this option is used to refer to definite IDEA tool in the process of the program is running automatically compile, finally click the “Close” button to complete the setting

  • Test whether the hot deployment is successful

Results a

To test whether the hot deployment of the configuration works, next, without closing the current project, change the return value of the request handler hello() in the DemoController class to “Hello Spring Boot13333” and save, If you view the console information, you can find that the project can be automatically built and compiled, indicating that the hot deployment of the project has taken effect

The results of the second