How to use Feed4junit for data and code separation of Java unit tests

Source: Internet
Author: User
Tags arrays

Feed4junit and JUnit

Often, there is a large number of such interfaces in the business logic of the application: they accept different inputs and then perform or validate, or process, to complete the same process. For example, the site's login entry, username and password are limited in length, also has the ability to allow special characters, and so on, so in the process of our unit testing, according to different lengths of user names and passwords, as well as different combinations of characters, only need to provide the same test code structure, you can complete the test, Different only test data and expectations, but because each test method in the input parameters are different, we have to write a separate test case for each input group, resulting in a large number of redundant code, very inconvenient to maintain.

Based on the scenarios above, JUnit 4 provides parameterized features that enable testing of the same test code for different data entry, as shown in Listing 1:

Listing 1. JUnit 4 parameterized Test code sample

Package sample.test;
    
Import static org.junit.Assert.assertEquals;
Import Java.util.Arrays;
    
Import java.util.Collection;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.junit.runners.Parameterized;
    
Import Org.junit.runners.Parameterized.Parameters;
    
Import sample.code.UserAccess; * * Junit-parameter Test Sample */@RunWith (Parameterized.class) public class Junitsample {private String U
    Ser
    Private String PW;
    
    Private Boolean expected; 
                @Parameters public static Collection Datagenerater () {return arrays.aslist (new object[][] { {"User01", "123456", true}, {"HelloWorld", "123456", false}, {"David", "Re*ads", Fals
    e}, {"Goodone", "Onegood", True}});
        Public junitsample (string user, String pw, Boolean expected) {this.user = user;
        THIS.PW = PW;
    this.expected = expected; } @Test Public void Testaccesscheck () {assertequals (expected, Useraccess.accesscheck (user, PW)); }
}

As you can see from the example code above, JUnit 4 produces data by using a static method of Collection for the return type of a tag @Parameters annotation, passing the test data to the test method through a variable to complete the test of multiple data entry. But with the needs of the business, testers need to constantly increase test data and modify existing test data, JUnit 4 provides the hard coding method has become more cumbersome and inconvenient, data and code separation is particularly important.

Fortunately, the Feed4junit in this article solves the problem of separation of data and code well, Feed4junit is an extension of the JUnit test framework that makes your unit tests easier to write and maintain by manipulating test data from files and from different data sources.

This article will show you the installation of feed4junit and the implementation of test code and data separation by example, and note that the sample code for this article is all based on a very simple user logon validation class, and assumes that you are using Eclipse as your IDE, see Listing 2 class Code:

Listing 2. Instance class

Package 

Sample.code;
    
public class UserAccess {
    //simple validation for user name and password public
    static Boolean AccessCheck (String UserName, String password) {
        if (username.length () <= 4 | | username.length () > 8) return
            false;
        if (Password.length () <= 4 | | | password.length () > 8) return
            false;
        if (Username.contains ("@")) return
            false;
        if (Password.contains ("*")) return
            false;
        return true;
    }

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.