Project development issues from several issues related to JUnit encountered during code review

Source: Internet
Author: User
Tags execution rollback try catch

1, continuous integration, a unit test failed to find the author.

With the continuous flow of project members and the increasing number of test cases as the project develops, the author of the search code is a definitely work, how can you quickly and accurately navigate to the code developer? Java provides the @author to meet this requirement;

To develop additional and modified unit tests, you must annotate the developer to facilitate quick positioning.

2, the unit test method can only be executed once.

A colleague writes the code, calls the deletion method, passes in the primary key ID, deletes this record, the result executes again the time error, the object does not exist; There was a problem with the JUnit Unit test framework, and no default rollback was set. Just add the following annotations:

@TransactionConfiguration

(transactionmanager= "TransactionManager", defaultrollback=true)  
@ Transactional

Of course, you can also specify at the method level (generally not recommended):

@Rollback (True)

3, the unit test method is not asserted.

@Test public
void Testfindpeople () {  
    peoplequery pquery = new Peoplequery ();  
    Pquery.setname ("Zhangsan");  
    Pquery.setlevel (4);  
    Peopledao.findpeople (pquery);  
}

Code as above, do not know what the purpose of such unit testing, there is no business judgment logic.

As a result, junit test methods must be asserted (some extreme exclusions, such as test timeouts, exceptions, etc.) to meet business and logical requirements.

4, the method execution error, but junit explicit test success (appear green bar)

@Test public
void TestException () {  
    try {  
        int i = 10/0;  
        Assert.assertequals (2,i);             
    } catch (Exception e) {  
        e.printstacktrace ();  
    }         
}

Code such as, by 0 apart, you must know that the sentence will be an error, but the result, JUnit prompts "execution success." Why, then?

It turns out that after the error is 0, the statement bypasses the assertion, enters into the catch statement, and after printing the exception, the unit test finishes. No, not at all. Assertion!!!

JUNIT4 Support (the test fails if an exception occurs). Therefore, you do not need to add a try catch statement or an assertion in a catch statement block; And if you want to test for exceptions, you can use parameters to do so.

The above questions have added some of their own ideas.

The above issues are sufficient to reflect some of the problems of the development team:

1, development members of junit knowledge too little;

2, the development of the members see the problem, but did not reflect, it shows that the development team enthusiasm is not high;

3, reflected the problem, but other members are still the case, indicating that the team communication is not enough or not a learning team, did not provide a certain amount of communication, study time.

4, the project leader only concerned about the function is normal, everything else regardless. Alas, speechless.

In this, I hope that when you encounter the above problems, can actively solve, rather than negative face.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.