JUnit Study Notes (III): Test-driven development and cobertura framework

Source: Internet
Author: User
Test-driven development test-driven development (TDD) is a new development method different from the traditional software development process. It requires that you write the test code before writing the code for a function, and then write only the functional code that passes the test to promote the entire development. This helps write concise, usable, and high-quality code and accelerates the development process.

Traditional development process:
Code ---> test ---> repeat ---> submit

Test-driven development process:
Test ---> code ---> repeat ---> submit

The following is a simple example of test-driven development for user management:

1. Define the object class user first:

package com.fjnu.model;public class User {private String username;private String password;private String nickname;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname = nickname;}public User() {super();// TODO Auto-generated constructor stub}public User(String username, String password, String nickname) {super();this.username = username;this.password = password;this.nickname = nickname;}}

2. Define the data operation layer interface iuserservice:

package com.fjnu.service;import com.fjnu.model.User;public interface IUserService {public void add(User user);public void delete(String username);public User load(String username);public User login(String username, String password);}

3. Define another exception class userexception to inherit runtimeexception.

4. With this, you can write the test class testuserservice for the iuserservice interface. This test class is similar to the white box test. You must take into account the possible situations of various branches and write the test code:

Package COM. fjnu. service; import static Org. JUnit. assert. *; import Org. JUnit. before; import Org. JUnit. test; import COM. fjnu. model. user; import COM. fjnu. model. userexception; public class testuserservice {private iuserservice us; private user baseuser; @ beforepublic void setup () {// initialize US = new userservicebymap (); baseuser = new user ("admin ", "123", "Administrator");} private void assertuserequals (User U, user Tu) {assert Equals ("the add method has an error! ", U. GetUserName (), tu. GetUserName (); assertequals (" the add method has an error! ", U. getnickname (), tu. getnickname (); assertequals (" the add method has an error! ", U. getPassword (), tu. getPassword () ;}@ testpublic void testadd () {user u = baseuser; us. add (U); User Tu = us. load ("admin"); assertnotnull (TU); assertuserequals (u, tu); // fail ("add the added test code ");} @ test (expected = userexception. class) Public void addexistusername () {us. add (baseuser); User Tu = new user ("admin", "1234", "alskdf"); US. add (TU) ;}@ testpublic void testdelete () {us. add (baseuser); User Tu = us. load (baseuser. getUserName (); assertnotnull (TU); US. delete (baseuser. getUserName (); Tu = us. load (baseuser. getUserName (); assertnull (TU) ;}@ testpublic void testlogin () {us. add (baseuser); string username = baseuser. getUserName (); string Password = baseuser. getPassword (); User Tu = us. login (username, password); assertuserequals (baseuser, tu) ;}@ test (expected = userexception. class) Public void testnotexistsuserlogin () {us. add (baseuser); string username = "admin1"; string Password = "123"; us. login (username, password) ;}@ test (expected = userexception. class) Public void testpassworderroruserlogin () {us. add (baseuser); string username = "admin"; string Password = "1235"; us. login (username, password );}}

5. After writing the test class, you can implement the iuserservice interface:

Package COM. fjnu. service; import Java. util. hashmap; import Java. util. map; import COM. fjnu. model. user; import COM. fjnu. model. userexception; public class userservicebymap implements iuserservice {private Map <string, user> US = new hashmap <string, user> (); @ overridepublic void add (User user) {// todo auto-generated method stubif (load (user. getUserName ())! = NULL) {Throw new userexception ("the user name already exists");} us. put (user. getUserName (), user) ;}@ overridepublic void Delete (string username) {// todo auto-generated method stubus. remove (username) ;}@ overridepublic user load (string username) {// todo auto-generated method stubreturn us. get (username) ;}@ overridepublic user login (string username, string password) {// todo auto-generated method stubuser u = load (username ); If (u = NULL) throw new userexception ("the user name does not exist"); If (! U. GetPassword (). Equals (password) throw new userexception ("incorrect user password"); Return U ;}}

Finally:

Cobertura framework: When testing code coverage with test-driven development, there will be a problem, that is, you do not know whether your test code covers all the situations, or do not know how much coverage. Cobertura is an open-source tool that detects basic code and observes the code executed and not executed when testing the package runtime to measure test coverage. In addition to finding untested code and bugs, cobertura can optimize the code by marking useless and unexecuted code. It can also provide internal information for actual API operations. More detailed introduction and usage: Click the open link and click the open link

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.