1. Development stage
Unit Testing
The most important thing during the development stage is unit testing. Spring Boot's support for unit testing has been perfected.
1. Add spring-boot-starter-test package reference to the pom package
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2. Develop test classes. Taking the simplest helloworld as an example, you need to add: @RunWith() and @SpringBootTest annotations to the class head of the test class. Add @Test at the top of the test method. Finally, right-click on the method to run.
@RunWith() @SpringBootTest public class ApplicationTests { @Test public void hello() { ("hello world"); } }
In actual use, you can inject data layer code or Service layer code according to the normal use of the project for testing and verification. spring-boot-starter-test provides many basic usages, and what is even more rare is that it adds support for Controller layer testing.
//Simply verify whether the result set is correct(3, ().size()); //Verify the result set, prompt("Error, the correct return value is 200", status == 200); ("Error, the correct return value is 200", status != 200);
MockMvc has been introduced to support testing of the Controller layer. The simple example is as follows:
public class HelloControlerTests { private MockMvc mvc; //Initialize execution @Before public void setUp() throws Exception { mvc = (new HelloController()).build(); } //Verify whether the controller responds normally and prints the return result @Test public void getHello() throws Exception { (("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(().isOk()) .andDo(()) .andReturn(); } //Verify whether the controller responds normally and determine whether the return result is correct @Test public void testHello() throws Exception { (("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World"))); } }
Unit testing is the first barrier to verify your code. You should develop the habit of unit testing every time you write a part of your code. Don’t wait until all of them are integrated before testing. After integration, it is easy to miss the bugs at the bottom of the code because you pay more attention to the overall operation effect.
2. Integration testing
After the overall development is completed, the integration test is entered. The startup entrance of the Spring Boot project is in the Application class. You can start the project by running the run method directly. However, during the debugging process, we must constantly debug the code. If you need to manually restart the service every time the code is modified, it will be very troublesome. Spring Boot provides hot deployment support very considerately, which is very convenient for debugging and use in web projects.
pom needs to add the following configuration:
<dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
After adding the above configuration, the project supports hot deployment, which is very convenient for integration testing.
3. Going production and going online
Actually, I think this stage should be relatively simple and generally divided into two types: one is to package it into a jar package and execute it directly, and the other is to package it into a war package and put it under the tomcat server.
3.1. Make it into a jar package
If you are using maven to manage your project, execute the following command.
cd project and directory (and same level)
mvn clean package ## or execute the following command## Package after excluding the test codemvn clean package -=true
After the packaging is completed, the jar package will be generated in the target directory. The name is usually the project name + version number.jar
Start the jar package command
java -jar target/spring-boot-scheduler-1.0.
In this way, as long as the console is closed, the service will not be accessible. Let's start the following by running in the background:
nohup java -jar target/spring-boot-scheduler-1.0. &
You can also choose to read different configuration files when starting
java -jar --=dev
You can also set jvm parameters at startup
java -Xms10m -Xmx80m -jar &
3.2. Make it into war bag
The first type can be used to export the war package through eclipse development tool, and the other type is to use commands to complete it. Here we mainly introduce the latter one.
1. Maven project, modify the pom package. Modify jar to war
<packaging>war</packaging>
2. Exclude tomcat when packaging.
<dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
Here, set the scope property to provided so that the JAR package will not be included in the final WAR, because servers such as Tomcat or Jetty will provide relevant API classes at runtime.
3. Register the startup class creation, inherit SpringBootServletInitializer, override configure(), and register the startup class Application. When an external web application server builds a Web Application Context, the startup class will be added.
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return (); } }
Final execution
mvn clean package -=true
The project name + version number.war file will be generated in the target directory, and copy it to the tomcat server to start.
4. Production Operation and Maintenance
To view the value of JVM parameters, you can use the jinfo command provided by Java:
jinfo -flags pid
Let’s see what jar is used after starting gc, the new generation, and the old generation are used in batches. The examples are as follows:
-XX:CICompilerCount=3 -XX:InitialHeapSize=234881024 -XX:MaxHeapSize=3743416320 -XX:MaxNewSize=1247805440 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=78118912 -XX:OldSize=156762112 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC
XX:CICompilerCount: The maximum parallel compile number
-XX:InitialHeapSize and -XX:MaxHeapSize: Specifies the initial and maximum heap memory size of the JVM
-XX:MaxNewSize: Maximum allocable size for the next generation memory in the JVM heap area
-XX:+UseParallelGC: Garbage collection uses Parallel collector
How to restart?
Simple and crude, kill the process and start the jar package again
ps -ef|grep java ##Get the pid for Java programskill -9 pid ## Restart againJava -jar
Of course, this method is more traditional and violent, so it is recommended that you use the following method to manage it.
If the script is executed, if it is using maven, the following configuration is required.
<plugin> <groupId></groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin>
Startup method:
1. You can start directly by ./
2. Register as a service
You can also make a soft link to point to your jar package and add it to it, and then start it with commands.
example:
ln -s /var/yourapp/ /etc//yourapp chmod +x /etc//yourapp
This way you can use the stop or restart command to manage your application.
/etc//yourapp start|stop|restart
or
service yourapp start|stop|restart
At this point, the Spring Boot project has been introduced to test, coordinated and packaged and put into production. You can find time to study the automated operation and maintenance of Spring Boot and the combination of Spring Boot and Docker.
This is the end of this article about how SpringBoot elegantly conducts test packaging and deployment. For more relevant SpringBoot test packaging and deployment content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!