Test Development Learning

1. Trends in software quality

Modern Software Development Model

The problem with the traditional waterfall model is that the delivery cycle is too long for the needs of users, and the agile model breaks a big problem into many small problems.

Each iteration emphasizes delivering local value, which is also called MVP Minimum Delivery Value. It is difficult to select MVP

A compromise "pseudo" agile test

By automating the testing process to keep up with the speed required for delivery, the first step is required for testers to program. Secondly, due to the simplification of the Internet in the current business, the technical requirements for R&D personnel are much higher than for business, and the matching testing technology is also skewed, which results in testers having greater technical requirements than business personnel.

Continuous integration, continuous publishing, and continuous delivery have become the technical solution to the R&D end based on DevOps. The disadvantage of such DevOps is that it does not introduce the demand side, but it achieves fast debugging in the R&D area assuming that the demand is an MVP, which is implemented quickly in the delivery R&D area and handed over to users for quick debugging.

Common delivery modes are as follows, and at this end, to improve the efficiency of testing, test is divided into tests, which is born. Layered testing is designed in the technical domain and requires a lot of R&D, so test development skills stack is born.

2. JUINT

Generate test methods

Add JUINT5 Dependency

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.6.2</version>
    <scope>test</scope>
</dependency>

1. Select method, right-click

2. Select test

 

3. Select OK

Drivers and Assertions

class ResultCodeTest {

    @Test
    void setCode() {
    }

    @Test
    void Code200(){
        ResultCode expected = ResultCode.OK;
        // Use assertions to judge
        Assertions.assertEquals(expected.getCode(),200,"Assert 200 Status");
    }
}

Complete some initialization or destruction operations before and after test execution because the framework creates a separate instance for each test, and no test instances are created when the @BeforeAll / @AfterAll method is executed. Therefore, these two methods must be defined as static methods

- @BeforeAll executes only once, before all tests and @BeforeEach annotation methods

- @BeforeEach is executed before each test execution (Juint4 is @Before)

- @AfterEach is executed after each test execution (Junit4 is @After)

- @AfterAll executes only once, after all tests and @AfterEach annotation methods

Based on SpringBootTest

Used to test DAO and sevice layers, direct JUNIT testing will fail

Test impl

Select queryById generation test method

Annotate @SpringBootTest on the Test class

Inject UserServiceimpl

 

Write test logic

@SpringBootTest
class UserServiceImplTest {
    @Resource
    private UserService userService;

    @Test
    void queryById() {
        User myu = new User();
        myu = this.userService.queryById(1);
        Assertions.assertEquals(myu.getUsername(),"cloud");
        /*myu.setId(13);
        myu.setUsername("red13");
        myu.setPassword("passw0ld");
        myu.setDemo("testjunit");
        this.userService.insert(myu);*/
    }
}

Test success:

Test Failure: Change cloud to cloud 1

Test the insertion function:

@Test
    void insertUser(){
        User myu = new User();
        myu.setId(13);
        myu.setUsername("red13");
        myu.setPassword("passw0ld");
        myu.setDemo("testjunit");
        this.userService.insert(myu);
    }

Execution Result: Successful execution

View Database: Insert Successful

Disadvantage: True insertion of data into the database

Improvement

Method with @Transactional annotation means that it uses transactions and rolls back automatically after each execution

If you don't want to rollback, you can add @Rollback(false) to the method, which is true by default

Re-execute the above method: Change 13 to 14 to execute

View the database:

No data inserted into the database

Tags: unit testing

Posted on Tue, 21 Sep 2021 12:07:29 -0400 by RHolm