Development Phase Unit Testing
The most important part of the development phase is unit testing, and Springboot support for unit testing is well established.
1. Add the Spring-boot-starter-test package reference in the POM package
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-test</artifactid> <scope>test</scope></dependency>
2、开发测试类
In the simplest case of HelloWorld, the class header of the test class needs to be added: @RunWith(SpringRunner.class)
and @SpringBootTest
annotations,
Add it at the top of the test method @Test
, and then right-click on the method to run it.
@RunWith (Springrunner. Class) @SpringBootTestpublicclass applicationtests { @Test publicvoid hello () { System.out.println ("Hello World");} }
In actual use, you can inject DAO layer code or service layer code to test and verify according to the normal use of the project, Spring-boot-starter-test
Provides a lot of basic usage and, more importantly, adds support for controller layer testing.
// simply verify that the result set is correct assert.assertequals (3, Usermapper.getall (). Size ()); // validating result sets, prompting assert.asserttrue ("error, correct return value is $", status = =); Assert.assertfalse ("error, correct return value is $", status! = 200);
The introduction of the MockMvc
support for the controller layer test, a simple example is as follows:
Public classhellocontrolertests {PrivateMOCKMVC MVC; //Initialize Execution@Before Public voidSetUp ()throwsException {MVC= Mockmvcbuilders.standalonesetup (NewHellocontroller ()). build (); } //Verify that the controller responds properly and prints the returned results@Test Public voidGethello ()throwsException {mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept (Mediatype.application_json)). Andexpect (Mockmvcresultmatchers.status (). IsOk ()). A Nddo (Mockmvcresulthandlers.print ()). Andreturn (); } //Verify that the controller is responding properly and that the returned results are correct@Test Public voidTesthello ()throwsException {mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept (Mediatype.application_json)). Andexpect (Status (). IsOk ()). Andexpect (Content (). str ING (Equalto ("Hello World"))); } }
Unit testing is the first barrier to validating your code, and it's a habit to do unit testing every part of your code, not wait until all the integrations are tested.
After integration because more attention to the overall performance, it is easy to omit the code underlying bugs.
Integration Testing
After the overall development is completed, enter the integration test, the Spring Boot Project launch Portal in the application class, directly running the Run method can start the project,
But in the process of debugging we certainly need to constantly debug the code, if each time you modify the code need to manually restart the service is very troublesome,
Spring boot is a very thoughtful support for hot deployment, and it's easy to debug in a Web project.
The POM needs to add the following configuration:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency></dependencies> <build> <plugins> <plugin> < Groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</ artifactid> <configuration> <fork>true</fork> </ Configuration> </plugin></plugins></build>
After adding the above configuration, the project supports a hot deployment and is very convenient for integration testing.
Production line
In fact, I think this stage, it should be relatively simple is generally divided into two kinds, one is packaged into a jar package to execute directly, the other is packaged into a war package
Put it under the Tomcat server.
Make a jar package
If you are using MAVEN to manage your project, execute the following command to
Package-dmaven.test.skip=true
After the package is complete, the jar package is generated to the target directory, and the name is typically the project name + version number. jar
Start the jar Package command
Java-jar Target/spring-boot-scheduler-1.0.0.jar
In this way, the service cannot be accessed as long as the console shuts down. Below we use the way to run in the background to start:
Nohup Java-jar Target/spring-boot-scheduler-1.0.0.jar &
You can also choose to read different configuration files at startup
Java-jar App.jar--spring.profiles.active=dev
Gradle
If you are using Gradle, use the following command to package
-jar Build/libs/mymodule-0.0.1-snapshot.jar
Fight a war bag
A war package can generally be implemented in two ways, the first of which is to export a war package through eclipse, and the other is to use commands to
Completed, here is the main introduction of the latter
1. Maven project, modify POM Package
Will
<packaging>jar</packaging>
Switch
<packaging>war</packaging>
2. When packing, exclude Tomcat.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-web</artifactid></dependency><dependency> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope></dependency>
Set the scope property here to provided so that the jar package is not included in the resulting war, because servers such as Tomcat or jetty
The associated API classes will be available at run time.
3. Registration Startup Class
Create Servletinitializer.java, inherit Springbootservletinitializer, overwrite configure (), register the startup class application. External
When the Web application server builds the Web application context, the startup class is added.
Public class extends Springbootservletinitializer { @Override protected Springapplicationbuilder Configure (Springapplicationbuilder application) { return application.sources (application. Class);} }
Final execution
Package -dmaven.test.skip=True
will be generated under target directory: Project name + version number. war file, copy to Tomcat server to start.
Gradle
If you are using Gradle, the basic step-up is the same, Build.gradle added support for the war, excluding Spring-boot-starter-tomcat:
' War '... dependencies { compile ("Org.springframework.boot:spring-boot-starter-web:1.4.2.release" { exclude mymodule:"Spring-boot-starter-tomcat" }} ...
Use the build command again
Gradle Build
The war is generated under the Build\libs directory.
Production operations view values for JVM parameters
You can customize the Jinfo command with Java:
Jinfo-flags PID
To see what GC, Cenozoic, and older generations of memory were used after the jar was started, as an example:
-
- xx : cicompilercount = 3 - xx : Initialheapsize = 234881024 - XX : maxheapsize = 3743416320 - XX : maxnewsize = 1247805440
-
span> - XX : minheapdeltabytes = 524288 - XX : NewSize = 78118912 - XX : Oldsize = 156762112 - XX : + usecompressedclasspointers
-
span> - XX : + usecompressedoops - XX : + usefastunorderedtimestamps - XX : + useparallelgc
-XX:CICompilerCount
: Maximum number of parallel compilations
-XX:InitialHeapSize
and -XX:MaxHeapSize
: Specify the initial and maximum heap memory sizes for the JVM
-XX:MaxNewSize
: Maximum assignable size of cenozoic memory in the JVM heap area
...
-XX:+UseParallelGC
: Garbage collection using parallel collector
How to restart
Simple Rough
Kill the process directly and start the jar package again
Ps-ef| -9-jar Xxxx.jar
Of course this approach is more traditional and violent, so it is recommended that you use the following method to manage
Script execution
If you are using MAVEN, you need to include the following configuration
<plugin> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-maven-plugin</artifactid> <configuration> <executable>true</ Executable> </configuration></plugin>
If you are using Gradle, you need to include the following configuration
springboot { true}
Starting mode:
1, can be directly ./yourapp.jar
to start
2, registered as a service
You can also make a soft link to your jar package and add it to the init.d
, then start with a command.
INIT.D Example:
Ln-s/var/yourapp/yourapp.jar/etc/init.d/+x/etc/init.d/yourapp
This allows you to use stop
or restart
command to manage your app.
/etc/init.d/yourapp Start|stop|restart
Or
Service YourApp Start|stop|restart
To this springboot project how to test, joint and packaged production has been introduced, you can find time to study the Springboot automation operations, and
The use of spring boot and Docker combined.
Example code: Https://github.com/ityouknow/spring-boot-starter
Springboot (12): Springboot How to test a packaged deployment