当前位置:网站首页>Yyds dry goods inventory junit5 learning II: assumptions class

Yyds dry goods inventory junit5 learning II: assumptions class

2022-06-24 22:22:00 Programmer Xinchen

Welcome to visit mine GitHub

Here we classify and summarize all the original works of Xinchen ( Including supporting source code ): https://github.com/zq2599/blog_demos

An overview of this article

This article is about 《JUnit5 Study 》 The second part of the series , Learn an important knowledge point :Assumptions class , Only by understanding them , Only then can continue the later study , The whole chapter is as follows :

  1. Assertions and Assumptions brief introduction
  2. Write a piece of code to compare the effect
  3. Assumptions code
  4. View the execution results

Source download

  1. If you don't want to code , Can be in GitHub Download all the source code , The address and link information is shown in the following table :
name link remarks
Project home page  https://github.com/zq2599/blog_demos The project is in progress. GitHub Home page on
git Warehouse address (https) https://github.com/zq2599/blog_demos.git The warehouse address of the source code of the project ,https agreement
git Warehouse address (ssh) [email protected]:zq2599/blog_demos.git The warehouse address of the source code of the project ,ssh agreement
  1. This git Multiple folders in project , The application of this chapter in junitpractice Under the folder , As shown in the red box below :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _junit

  2. junitpractice It's a father son structure project , The code of this article is in assertassume In the subproject , Here's the picture :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_02

Assertions and Assumptions brief introduction

Assumptions and Assertions Easy to confuse , So here we learn by comparing them :

  1. Assertions The assertion class , It provides a lot of static methods , for example assertTrue, If assertTrue The input parameter of is false, Will throw AssertionFailedError abnormal ,Junit The method that throws this exception is judged as failure ;
  2. Assumptions The hypothetical class , It provides a lot of static methods , for example assumeTrue, If assumeTrue The input parameter of is false, Will throw TestAbortedException abnormal ,Junit The method that throws this exception is judged as skipping ;
  3. To put it simply ,Assertions To throw an exception means that the test fails ,Assumptions Means that the test is skipped ( Why is it called " skip "? because mvn test The execution result of is marked as Skipped);

Write a piece of code to compare the effect

  1. Code is the best way to verify it , As shown below , There are four ways ,assertSuccess Do not throw AssertionFailedError abnormal ,assertFail Throw out AssertionFailedError abnormal ,assumpSuccess Do not throw TestAbortedException abnormal ,assumpFail Throw out TestAbortedException abnormal
package com.bolingcavalry.assertassume.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@SpringBootTest
@Slf4j
public class AssertAssumpTest {

    /** *  The simplest successful use case  */
    @Test
    void assertSuccess() {
        assertEquals(2, Math.addExact(1,1));
    }

    /** *  The simplest failure use case  */
    @Test
    void assertFail() {
        assertEquals(3, Math.addExact(1,1));
    }

    /** * assumeTrue Use cases that don't throw exceptions  */
    @Test
    void assumpSuccess() {
        // assumeTrue If the input parameter of the method is true, No exception will be thrown , Later code will continue to execute 
        assumeTrue(true);
        //  If you print this log , prove assumeTrue Method does not throw an exception 
        log.info("assumpSuccess Of assumeTrue Execution completed ");
        //  Next is the normal unit test logic 
        assertEquals(2, Math.addExact(1,1));
    }

    /** * assumeTrue Use cases that throw exceptions  */
    @Test
    void assumpFail() {
        // assumeTrue If the input parameter of the method is false, Will throw TestAbortedException abnormal , It won't be implemented later 
        assumeTrue(false, " Not through assumeTrue");
        //  If you print this log , prove assumpFail Method does not throw an exception 
        log.info("assumpFail Of assumeTrue Execution completed ");
        //  Next is the normal unit test logic , But because of the exception thrown earlier , No more 
        assertEquals(2, Math.addExact(1,1));
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  1. Click the red box in the figure below to execute unit test :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_03
  2. The results are as follows :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _ unit testing _04
  3. in addition , stay target Catalog , You can see surefire Unit test reports generated by plug-ins TEST-com.bolingcavalry.assertassume.service.impl.AssertAssumpTest.xml, As shown in the figure below ,testcase In the node skipped node :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _junit_05
  • The above comparative verification shows again that Assertions and Assumptions The difference between : Are used to compare the expected value with the actual value , When the expected value is inconsistent with the actual value ,Assertions The result of the test is execution failure ,Assumptions The test result is to skip ( Or ignore );

Assumptions actual combat

It's clear Assertions and Assumptions The difference between , Next, strike while the iron is hot , Study Assumptions Several important static methods in class :assumeTrue、assumingThat

  1. The simplest usage is as follows , It can be seen that only assumeTrue Don't throw exceptions , hinder log.info Will execute :
    @Test
    @DisplayName(" The most common assume usage ")
    void tryAssumeTrue() {
        assumeTrue("CI".equals(envType));

        log.info("CI The environment will print assumeTrue");
    }

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  1. assumeTrue Acceptable Supplier Type as the second input , If assumeTrue Failure will take the content of the second parameter as a failure prompt :
@Test
    @DisplayName("assume Failure with custom error message ")
    void tryAssumeTrueWithMessage() {
        //  The second parameter is Supplier Realization , The returned content is used as a hint to skip the use case 
        assumeTrue("CI".equals(envType),
                () -> " Environment mismatch and skip , The current environment :" + envType);

        log.info("CI The environment will print tryAssumeTrueWithMessage");
    }

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

The effect is as follows :
#yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_06
3. There's another. assumingThat Method , Acceptable Executable Type as the second input , If the first input is true Will execute Executable Of execute Method , Be careful assumingThat Characteristics of the method : Don't throw exceptions , So the method it's in won't be skipped , This is the assumeTrue Compared with the biggest difference (assumeTrue Once entered, it is false Will throw an exception , Its method is marked as skip ):

    @Test
    @DisplayName("assume Execute the specified logic on success ")
    void tryAssumingThat() {
        //  The second parameter is Executable Realization ,
        //  When the first parameter is true when , Executing the second parameter execute Method 
        assumingThat("CI".equals(envType),
                () -> {
                    log.info(" This line is only in CI The environment will print ");
                });

        log.info(" It will be printed in any environment tryAssumingThat");
    }

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • Next, let's execute the above code , Look at the effect ;

perform Assumptions Code

  1. Do the preparatory work first , This actual battle springboot Engineering, assertassume, We are working on resources Add two configuration files to the directory :application.properties and application-test.properties, The location is as follows :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_07
  2. application-test.properties The contents are as follows :
envType:CI

     
  • 1.
  1. application.properties The contents are as follows :
envType:PRODUCTION

     
  • 1.
  1. The complete unit test classes are as follows , Through annotation ActiveProfiles, Specified the use of application-test.properties Configuration of , therefore envType The value of is CI
package com.bolingcavalry.assertassume.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assumptions.assumingThat;

@SpringBootTest
@Slf4j
@ActiveProfiles("test")
public class AssumptionsTest {

    @Value("${envType}")
    private String envType;

    @Test
    @DisplayName(" The most common assume usage ")
    void tryAssumeTrue() {
        assumeTrue("CI".equals(envType));

        log.info("CI The environment will print assumeTrue");
    }

    @Test
    @DisplayName("assume Failure with custom error message ")
    void tryAssumeTrueWithMessage() {
        //  The second parameter is Supplier Realization , The returned content is used as a hint to skip the use case 
        assumeTrue("CI".equals(envType),
                () -> " Environment mismatch and skip , The current environment :" + envType);

        log.info("CI The environment will print tryAssumeTrueWithMessage");
    }

    @Test
    @DisplayName("assume Execute the specified logic on success ")
    void tryAssumingThat() {
        //  The second parameter is Executable Realization ,
        //  When the first parameter is true when , Executing the second parameter execute Method 
        assumingThat("CI".equals(envType),
                () -> {
                    log.info(" This line is only in CI The environment will print ");
                });

        log.info(" It will be printed in any environment tryAssumingThat");
    }
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  1. The results are shown in the following figure , so assume adopt , All the information has been printed out :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_08

  2. Next, put the... In the code ActiveProfiles Note out the line of notes , The red box is shown below :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _spring_09

  3. The results are as follows , so tryAssumingThat Methods are marked as successful , But from the log we can see assumingThat The second entry of executable Not implemented :
    #yyds Dry inventory #JUnit5 Study two :Assumptions class _ unit testing _10

  • thus ,Assumptions Class common method experience complete , The following sections will continue to learn about other common classes ;

Welcome to your attention 51CTO Blog : Xinchen, programmer

  On the way to study , You are not alone , Xinchen's original works are accompanied all the way …

原网站

版权声明
本文为[Programmer Xinchen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211301103055.html