Implementation of SpringBoot+TestNG Unit Test

  • 2021-10-25 06:36:54
  • OfStack

Directory background Interface test cases, design for input parameters: get down to business!

Background

Due to the tight schedule of development tasks and incomplete interface and basic data, even if a comprehensive interface test case is designed, it can not be fully and effectively tested; And because the direction of single interface test case design is to enter and exit parameters, Starting from entering parameters, it is required parameter verification, parameter type and parameter boundary value. Then there is the combination of parameters, For example, one interface has five parameters, 3 required, 2 not required, The data types are string, int, etc., and there are character length restrictions, so the number of such single-interface test cases can't be counted. If the number of parameters and parameter types become more, then this number is indescribable. Therefore, it is necessary to consider the tester's mastery of the method of interface test case design.

Interface test cases, designed for input parameters:

1. Parameters of numeric type

Equivalence class division: within the value range and outside the value range, how to understand this?
If the interface document describes which values or intervals this parameter should take, choose within or outside the range specified here
Boundary value analysis: the maximum and minimum are just right, the maximum +1, and the minimum-1, which is to find the boundary from the value range, and the maximum and minimum are the data type boundaries
Special value design: 0 or non-positive number, decimal may be designed
Traversal: There is no shortcut, exhaustion of its value interval, this 1 will be equivalence class, boundary value to filter out, do not have to be exhausted;

2. String type

String length
Equivalence class: inside and outside the value interval
Boundary value: the boundary of the specified range; Type boundary
Special value: This needs to be distinguished from special characters of string type, which means 0 or empty string, null
String content
Specific types: Chinese and English, upper and lower case, simplified and traditional
Special characters: emoji emoticon, punctuation operation symbol, other special characters of input method

3. Rare array or linked list types: For example, list type may be int []\ or string [], and his design method can't escape the above, so I won't repeat it here.

Design for business logic, design for parameters and other aspects, especially business logic 1 generally adopts positive use case design, and a small number of abnormal scenarios are designed through input parameters, and the design of output parameters can almost get the expected results in input parameters design.

Get down to business!

1. eclipse is used locally as java development tool, and python environment programming is supported at the same time, so Pycharm and IDEA are not used at the same time;


<!--  Create first springboot Framed maven Project pom Add the following dependencies, ide You can install plug-ins: spring assistant , 1 Key creation springboot Framed maven Project  -->

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.4.0</version><!-- 2.3.0.RELEASE Version optional  -->
   <relativePath/> <!-- lookup parent from repository -->
</parent>

<!-- springboot The main dependencies of the framework  -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <version>2.4.0</version>
</dependency>

tips: @ SpringBootApplication\ @ RestController\ @ RequestMapping and so on

2. Import the local development environment. eclipse needs to install the lombok plug-in externally. Add parameters to the eclipse configuration file eclipse. ini at the bottom:-javaagent: lombok. jar with download address, besides installation, and whether the java team configured dependencies in pom


<dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
    <version>1.18.12</version>
 <optional>true</optional>
</dependency>

3. I am a tester, and I have installed the jacoco plug-in in the original environment, so I also added the plug-in in the maven project < Incidentally, I mentioned another one that relies on cobertura to test the code coverage of java > :


<!--  Incidentally 1 Another under 1 Plug-ins for code coverage detection  -->
<dependency>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.7</version>
</dependency>

<plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.3</version>
      <configuration>
     <!-- Specify the build  .exec  Where files are stored  -->
     <destFile>target/coverage-reports/jacoco-unit.exec</destFile>
     <!--Jacoco  Is based on  .exec  File generates the final report, so you need to specify  .exec  The storage path of  -->
            <dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
     </configuration>
     <executions>
         <execution>
  <id>jacoco-initialize</id>
           <goals>
               <goal>prepare-agent</goal>
            </goals>
         </execution>
         <execution>
  <id>jacoco-site</id>
           <phase>package</phase>
            <goals>
             <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- tips:jacoco In maven The command executed in: mvn clean jacoco:prepare-agent install jacoco:report -Dmaven.test.failure.ignore=true -->

4. When using java to assist jmeter testing before, I am used to testng unit testing framework and have not done unit testing on springboot framework, so the following dependencies need to be added to pom file:


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

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>6.9.10</version>
</dependency>
<!--  Right-click selection Coverage as  Execute the framework. After running, view the coverage in the specified directory or the current console  -->

5. The development habit is to use junit4.x for unit testing, in which the plug-in spring-boot-starter-test is also used, but the inherited classes are not 1;


# junit4.x Is used as follows: 

import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class BaseTest  {
}

#  And replace it with testng Is used as follows: 
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;

@SpringBootTest
public class BaseTest extends AbstractTestNGSpringContextTests {
}

#  Also using @SpringBootTest Annotation, but it starts sprintboot Service not 1 Sample, testng Must inherit AbstractTestNGSpringContextTests Use; 
#  Extension: It can also be inherited AbstractTransactionalTestNGSpringContextTests Class, 2 Choose 1 ; 
# @SpringBootTest(classes={ It's yours SpringBoot Start app Class }) For example :@SpringBootTest(classes=UserApplication.class)

6. At this point, the test cases of junit framework can be converted into pleasant tests of testng framework by pressing Ctrl+1 on eclipse: Convert to TestNG (Annotations);
7. Expansion: Add swagger dependency and generate interface documents; lombok relies on getter and setter methods that support declarative fields, as well as tools for integrating Log logs.

Don't look at my article for the rest, but look at your own performance. The unit test cases are as follows:


package  com.text;

import xxx.xxx.xxx

public TestSampler extends BaseTest{
    
    @BeforeClass
    void bf(){
        //  Test write test preconditions, which only need to be executed 1 Data of times 
    }
    @Test
    void test_add(){
        //  Write the code to be tested here 
    }
    
//     . . . 
}

Related articles: