Add spring-boot-starter-test package references in package pom

  There are a lot of friends will ask me from time to time, how spring boot test project, how to deploy, what good deployment in production yet? This article will introduce how spring boot development, debugging, packaging production to the final on the line.
  
  Development phase
  
  unit tests
  
  during the development phase when the most important thing is the unit test, springboot support for unit tests are well established.
  
  1, the package added pom spring-boot-starter-test package references
  
  <dependency>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> spring-boot-starter-test </ the artifactId>
  
  <scope> Test < / scope>
  
  </ dependency>
  
  2, test -
  
  the simplest helloworld example, in the class-based test head needs to be added: @RunWith (SpringRunner.class) and @SpringBootTest annotation added at the top of the test method @Test We can finally run on the right-click method to run.
  
  @RunWith (SpringRunner.class)
  
  @SpringBootTest
  
  public class ApplicationTests {
  
  @Test
  
  public void Hello () {
  
  System.out.println ( "
  

  
  }
  
  Actual use, in accordance with normal use of the item to injection dao layer code or a service-level code and test validation, spring-boot-starter-test provides a lot of basic usage, even more rare is to increase support for Controller layer testing.
  
  // simple authentication result set is correct
  
  Assert.assertEquals (3, userMapper.getAll () size ().);
  
  // verify the result set, suggesting
  
  Assert.assertTrue ( "error, the correct return value 200", status == 200 is);
  
  Assert.assertFalse ( "error, the correct return value 200", status = 200); !
  
  introduced MockMvc support layer was tested for the Controller, the following simple example:
  
  public class HelloControlerTests {
  
  Private MockMvc MVC;
  
  // initialize performing
  
  @Before
  
  public void the setUp () throws Exception {
  
  MVC = MockMvcBuilders.standaloneSetup (HelloController in new new ()) Build ();.
  
  }
  
  // verify that the normal response and print controller returns the result
  
  @Test
  
  public void the getHello () throws Exception {
  
  mvc.perform (MockMvcRequestBuilders.get ( "/ Hello"). Accept (MediaType.APPLICATION_JSON))
  
  .andExpect (MockMvcResultMatchers.status (). ISOK (www.027yeshenghuowang.com/))
  
  .andDo (MockMvcResultHandlers.print ())
  
  . andReturn ();
  
  }
  
  // verify that the normal response controller and returns the determination result is correct
  
  @Test
  
  public void testHello () throws Exception {
  
  mvc.perform (MockMvcRequestBuilders.get ( "/ Hello") Accept (MediaType.APPLICATION_JSON).)
  
  . andExpect (Status () .isOk (http://xucaizxyl.com/))
  
  .andExpect (. Content () String (equalTo ( "the Hello World") http://www.wbjyl.cn/));
  
  }
  
  }
  
  units test is to verify the code you first barrier, to develop every part of writing code unit testing habits, do not wait until after the entire integration testing, after integration because it is more concerned about the overall operating results, it is easy to miss out on the bottom of the code bug .
  
  integration testing
  
  Overall developed into the later integration testing, start the entrance spring boot project in the Application class, run directly run method can start the project, but in the process of debugging we certainly need to continue to debug the code, if each modified once the code is required manually restart a service is very troublesome, spring boot gives a very intimate supports hot deployment, it is convenient to use in debugging web projects.
  
  pom configuration requires the following:
  
  <Dependencies>
  
  <dependency>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> Boot-Spring-DevTools <http://www.yigouyule2.cn/ / the artifactId>
  
  <optional> to true </ optional>
  
  </ dependency>
  
  </ Dependencies>
  
  <Build>
  
  <plugins>
  
  <plugin>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> Boot-Spring-Maven-plugin </ the artifactId>
  
  <Configuration >
  
  <the fork> to true </ the fork>
  
  <
  

  

  

  
  After adding the above configuration, the project will support hot deployment, integration testing is very convenient.
  
  Production on-line
  
  fact, I think at this stage, it should be relatively simple generally divided into two types; one is packaged into a jar package directly executed, the other is packaged into war package into the tomcat server.
  
  Labeled jar package
  
  if you are using maven to manage the project, execute the following command either
  
  cd project with the directory (and the same level pom.xml)
  
  mvn Clean Package Penalty for
  
  ## or execute the following command
  
  ## after exclusion of packaged test code
  
  mvn clean package -Dmaven.test.skip = true
  
  after complete package jar package will be generated to the next target directory, usually named after the project name + version .jar
  
  start jar package command
  
  java -jar target / spring-boot- scheduler-1.0.0 .jar
  
  this way, as long as the console is closed, you can not access the service. Here we use way to start running in the background:
  
  nohup target the Java -jar / the Spring-the Boot-Scheduler &-1.0.0.jar
  
  can also choose to read at startup different profiles
  
  java -jar app.jar - dev = spring.profiles.active
  
  Gradle
  
  If using gradle, the following command packaged
  
  gradle build
  
  Here Provided the scope attribute set, so WAR ultimately formed will not contain the JAR package because like Jetty or Tomcat server will provide an API classes at runtime.   3, registration startup class
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
  Creating ServletInitializer.java, inheritance SpringBootServletInitializer, covering configure (), the startup class Application Registration go. External web application server to build Web Application Context of the time, will start classes added to it.
  
  ServletInitializer the extends SpringBootServletInitializer {class public
  
  @Override
  
  protected SpringApplicationBuilder Configure (SpringApplicationBuilder file application) {
  
  return application.sources (Application.class);
  
  }
  
  }
  
  final performance
  
  mvn clean package -Dmaven.test.skip = true
  
  is generated in the target directory: project name + version .war file, copied to the tomcat server to start.
  
  gradle
  
  If using gradle, substantially the same as step-outs, the build.gradle added support of war, Boot-negative-Starter-Spring Tomcat:
  
  ...
  
  Apply plugin: 'war'
  
  ...
  
  Dependencies {
  
  the compile ( "org.springframework.boot: Spring-Boot-Starter-Web: 1.4.2.RELEASE") {
  
  the exclude MyModule: "Spring-Boot-Starter-Tomcat"
  
  }
  
  }
  
  ...
  
  reuse construction command
  
  Gradle Build
  
  WAR will generated in the build \ libs directory.
  
  Production operation and maintenance
  
  see the value JVM parameters
  
  can java command comes jinfo:
  
  jinfo -flags pid
  
  to see the jar after use to start what gc, the new generation, batch-old's memory is how much 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: maximum number of concurrent compilation
  
  -XX: InitialHeapSize and -XX: maxHeapSize: Specifies initial and maximum JVM heap memory size
  
  -XX: MaxNewSize: JVM heap memory area of the new generation of maximum allocation size
  
  ...
  
  -XX: + UseParallelGC: Parallel garbage collector using

  There are a lot of friends will ask me from time to time, how spring boot test project, how to deploy, what good deployment in production yet? This article will introduce how spring boot development, debugging, packaging production to the final on the line.
  
  Development phase
  
  unit tests
  
  during the development phase when the most important thing is the unit test, springboot support for unit tests are well established.
  
  1, the package added pom spring-boot-starter-test package references
  
  <dependency>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> spring-boot-starter-test </ the artifactId>
  
  <scope> Test < / scope>
  
  </ dependency>
  
  2, test -
  
  the simplest helloworld example, in the class-based test head needs to be added: @RunWith (SpringRunner.class) and @SpringBootTest annotation added at the top of the test method @Test We can finally run on the right-click method to run.
  
  @RunWith (SpringRunner.class)
  
  @SpringBootTest
  
  public class ApplicationTests {
  
  @Test
  
  public void Hello () {
  
  System.out.println ( "
  

  
  }
  
  Actual use, in accordance with normal use of the item to injection dao layer code or a service-level code and test validation, spring-boot-starter-test provides a lot of basic usage, even more rare is to increase support for Controller layer testing.
  
  // simple authentication result set is correct
  
  Assert.assertEquals (3, userMapper.getAll () size ().);
  
  // verify the result set, suggesting
  
  Assert.assertTrue ( "error, the correct return value 200", status == 200 is);
  
  Assert.assertFalse ( "error, the correct return value 200", status = 200); !
  
  introduced MockMvc support layer was tested for the Controller, the following simple example:
  
  public class HelloControlerTests {
  
  Private MockMvc MVC;
  
  // initialize performing
  
  @Before
  
  public void the setUp () throws Exception {
  
  MVC = MockMvcBuilders.standaloneSetup (HelloController in new new ()) Build ();.
  
  }
  
  // verify that the normal response and print controller returns the result
  
  @Test
  
  public void the getHello () throws Exception {
  
  mvc.perform (MockMvcRequestBuilders.get ( "/ Hello"). Accept (MediaType.APPLICATION_JSON))
  
  .andExpect (MockMvcResultMatchers.status (). ISOK (www.027yeshenghuowang.com/))
  
  .andDo (MockMvcResultHandlers.print ())
  
  . andReturn ();
  
  }
  
  // verify that the normal response controller and returns the determination result is correct
  
  @Test
  
  public void testHello () throws Exception {
  
  mvc.perform (MockMvcRequestBuilders.get ( "/ Hello") Accept (MediaType.APPLICATION_JSON).)
  
  . andExpect (Status () .isOk (http://xucaizxyl.com/))
  
  .andExpect (. Content () String (equalTo ( "the Hello World") http://www.wbjyl.cn/));
  
  }
  
  }
  
  units test is to verify the code you first barrier, to develop every part of writing code unit testing habits, do not wait until after the entire integration testing, after integration because it is more concerned about the overall operating results, it is easy to miss out on the bottom of the code bug .
  
  integration testing
  
  Overall developed into the later integration testing, start the entrance spring boot project in the Application class, run directly run method can start the project, but in the process of debugging we certainly need to continue to debug the code, if each modified once the code is required manually restart a service is very troublesome, spring boot gives a very intimate supports hot deployment, it is convenient to use in debugging web projects.
  
  pom configuration requires the following:
  
  <Dependencies>
  
  <dependency>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> Boot-Spring-DevTools <http://www.yigouyule2.cn/ / the artifactId>
  
  <optional> to true </ optional>
  
  </ dependency>
  
  </ Dependencies>
  
  <Build>
  
  <plugins>
  
  <plugin>
  
  <the groupId> org.springframework.boot </ the groupId>
  
  <the artifactId> Boot-Spring-Maven-plugin </ the artifactId>
  
  <Configuration >
  
  <the fork> to true </ the fork>
  
  <
  

  

  

  
  After adding the above configuration, the project will support hot deployment, integration testing is very convenient.
  
  Production on-line
  
  fact, I think at this stage, it should be relatively simple generally divided into two types; one is packaged into a jar package directly executed, the other is packaged into war package into the tomcat server.
  
  Labeled jar package
  
  if you are using maven to manage the project, execute the following command either
  
  cd project with the directory (and the same level pom.xml)
  
  mvn Clean Package Penalty for
  
  ## or execute the following command
  
  ## after exclusion of packaged test code
  
  mvn clean package -Dmaven.test.skip = true
  
  after complete package jar package will be generated to the next target directory, usually named after the project name + version .jar
  
  start jar package command
  
  java -jar target / spring-boot- scheduler-1.0.0 .jar
  
  this way, as long as the console is closed, you can not access the service. Here we use way to start running in the background:
  
  nohup target the Java -jar / the Spring-the Boot-Scheduler &-1.0.0.jar
  
  can also choose to read at startup different profiles
  
  java -jar app.jar - dev = spring.profiles.active
  
  Gradle
  
  If using gradle, the following command packaged
  
  gradle build
  
  Here Provided the scope attribute set, so WAR ultimately formed will not contain the JAR package because like Jetty or Tomcat server will provide an API classes at runtime.   3, registration startup class
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
  Creating ServletInitializer.java, inheritance SpringBootServletInitializer, covering configure (), the startup class Application Registration go. External web application server to build Web Application Context of the time, will start classes added to it.
  
  ServletInitializer the extends SpringBootServletInitializer {class public
  
  @Override
  
  protected SpringApplicationBuilder Configure (SpringApplicationBuilder file application) {
  
  return application.sources (Application.class);
  
  }
  
  }
  
  final performance
  
  mvn clean package -Dmaven.test.skip = true
  
  is generated in the target directory: project name + version .war file, copied to the tomcat server to start.
  
  gradle
  
  If using gradle, substantially the same as step-outs, the build.gradle added support of war, Boot-negative-Starter-Spring Tomcat:
  
  ...
  
  Apply plugin: 'war'
  
  ...
  
  Dependencies {
  
  the compile ( "org.springframework.boot: Spring-Boot-Starter-Web: 1.4.2.RELEASE") {
  
  the exclude MyModule: "Spring-Boot-Starter-Tomcat"
  
  }
  
  }
  
  ...
  
  reuse construction command
  
  Gradle Build
  
  WAR will generated in the build \ libs directory.
  
  Production operation and maintenance
  
  see the value JVM parameters
  
  can java command comes jinfo:
  
  jinfo -flags pid
  
  to see the jar after use to start what gc, the new generation, batch-old's memory is how much 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: maximum number of concurrent compilation
  
  -XX: InitialHeapSize and -XX: maxHeapSize: Specifies initial and maximum JVM heap memory size
  
  -XX: MaxNewSize: JVM heap memory area of the new generation of maximum allocation size
  
  ...
  
  -XX: + UseParallelGC: Parallel garbage collector using

Guess you like

Origin www.cnblogs.com/jpfss/p/10953399.html
Recommended