Lecture 6---Unit testing problems and solutions

Foreword:

        It's been a long time since I wrote a blog. I was busy some time ago, so I just kept the blog content. As February is approaching the annual leave, the task of the department is relatively less heavy, so we can make time to compile the usual records.

 

text:

1. How unit tests measure exception branches

There are three main methods for unit testing to test abnormal branches:

1.1、Try/catch with assert/fail

        In versions prior to JUnit4, exceptions were checked using try/catch blocks. Although this method is very old, it is still very effective. The main disadvantage is that it is easy to forget to write the fail() method after the catch block, which can lead to false positives if the expected exception is not thrown:

@Test
public void canVote_throws_IllegalArgumentException_for_zero_age() {
    Student student = new Student();
    try {
        student.canVote(0);
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage(), containsString("age should be +ve"));
    }
    fail("expected IllegalArgumentException for non +ve age");
}

 

1.2、@Test(expected…)

        The @Test annotation has an optional parameter, "expected" that allows you to set a subclass of Throwable. If you want to verify that the canVote() method throws the expected exception, we can write:

@Test(expected = IllegalArgumentException.class)
public void canVote_throws_IllegalArgumentException_for_zero_age() {
    Student student = new Student();
    student.canVote(0);
}

 

1.3、ExpectedException

        If you want to use the ExpectedException class in the JUnit framework, you need to declare the ExpectedException exception. Then you can verify the expected exception in a simpler way.

eg: Take the following method as an example, pass in two parameters of type Long and Map<String, String>

public SegmentQuotationVersion createSegmentQuotation(Long segmentDefinitionId, Map<String, String> dimensionValueMap);

The exception branch is "segment configuration id is empty":

private SegmentDefinition checkSegmentDefinition(Long segmentDefinitionId) {
        // 段配置ID为空 
        if(null == segmentDefinitionId){ 
            throw ServiceExceptionHelper.build(EnumError.QUOTE_SEGMENT_DEFINITION_ID_NULL); 
        } 
    }

The test method of the second way is as follows:

 @Rule
 public ExpectedException thrown = ExpectedException.none();

/**
 * 测试创建一个段报价方法  -- 传入空的段配置id
 */
 @DatabaseSetup("SegmentQuotationServiceTest_Init.xml")
 @Test
 public void testCreateSegmentQuotation_nullSegDefId() {
	thrown.expect(ServiceExceptionTestHelper.build(EnumError.QUOTE_SEGMENT_DEFINITION_ID_NULL));
	segmentQuotationService.createSegmentQuotation(null, buildDimensionValueMap());
}

 

2. Problems in the wiki and how to deal with them (this blog is updated at any time)

2.1. Since the windows system does not recognize case, the linux system recognizes case, so if the xml file is not case sensitive locally, there is no problem in running the unit test locally, but running the unit test on the jenkins server (linux system) will report an error .

 

2.2. The unit test class name must conform to *Test.java, otherwise maven-surefire-plugin will not generate the test result file, resulting in the jacoco-maven-plugin unit test statistics report indicating that the relevant code is not covered by unit tests.

eg: The unit test class names are named QuotationValidatorTest1 and QuotationValidatorTest2, then after building on jenkins, maven-surefire-plugin will not generate test result files for these two classes:

After changing the file name, the test result files of these two classes will be generated:

 

2.3. How does the unit test scan a class or not scan a class on jenkins?

Configured in the jenkins configuration as follows:

Note: The file here is a .class file instead of a .java file, because the .java file in eclipse needs to be compiled into a .class file, and the server virtual machine can recognize and run it, otherwise the virtual machine will not be able to achieve the effect if it is not recognized. .

 

Reference link:

1. Use JUnit to test the expected exception:

http://blog.csdn.net/tayanxunhua/article/details/20570457

2. JUnit uses ExpectedException for exception testing:

http://blog.csdn.net/z69183787/article/details/50442431

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324910393&siteId=291194637