`
longgangbai
  • 浏览: 7250194 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Feed4Junit的简单使用(五)数据来自动态约束数据

阅读更多

Feed4Junit官方地址:

http://databene.org/feed4junit.html

 

官方文档:

Generating constrained data dynamically

Feed4JUnit supports annotations defined in JSR 303, Java 7 and Benerator 0.7 for generating random data that matches constraints.

As an example see the @Pattern annotation as an example (javax.validation.constraints.Pattern):

@RunWith(Feeder.class)
public class RegexTest {

    @Test
    public void testSmoke(@Pattern(regexp = "[A-Z][a-z]{3,8}") String name) {
        System.out.println("name:" + name);
    }
    
} 

 

Annotation reference

Annotations marked bold are new in version 1.0:

Annotation Java Package Applicability
Description 
@AssertFalse javax.validation.constraints parameter Requires that a boolean parameter is false
@AssertTrue javax.validation.constraints parameter  Requires that a booöean parameter is true 
@DecimalMin javax.validation.constraints parameter  Requires that a number parameter is greater than or equals a minimum value 
@DecimalMax  javax.validation.constraints parameter  Requires that a number parameter is less than or equals a maximum value  
@Future  javax.validation.constraints parameter  Requires that a Date parameter is in the future 
@Min  javax.validation.constraints parameter  Requires that an integral number parameter is greater than or equals a minimum value 
@Max  javax.validation.constraints parameter  Requires that an integral number parameter is less than or equals a maximum value 
@NotNull  javax.validation.constraints parameter  Requires that a parameter is not null 
@Null  javax.validation.constraints parameter  Requires that a parameter is null 
@Past  javax.validation.constraints  parameter  Requires that a Date parameter is in the past 
@Pattern  javax.validation.constraints  parameter  Requires that a String parameter matches a regular expression 
@InvocationCount org.databene.benerator.anno  method  Limits the number of invocations to a test method 
@Source  org.databene.benerator.anno  method, parameter  Specifies a source file from which to read test data (e.g. CSV or Excel(TM) file) 
@Offset 
org.databene.benerator.anno method,
parameter
Makes Feed4Junit skip the first n data sets that are imported or generated 
@Distribution  org.databene.benerator.anno  parameter  Specifies a distribution to use for a number parameter 
@Granularity  org.databene.benerator.anno  parameter  Specifies the granularity of a number parameter (corresponds to Benerator's 'precision') 
@Nullquota  org.databene.benerator.anno  parameter  Specifies the quota of null values to generate for a parameter 
@Values  org.databene.benerator.anno  parameter  Specifies a comma-separated list of all supported values 
@Last  org.databene.benerator.anno  parameter 

Used for summary and cleanup functionality necessary after the last call to a test method: The last parameter of a mathod can be annotated with @Last and be made booloean. It will then receive a true value on the last invocation, otherwise false.

@Generator  org.databene.benerator.anno  method, parameter  Specifies a simple type generator for a parameter or an array generator for a complete parameter set 
@Database  org.databene.benerator.anno

class,
method

Defines a database to be used for data retrieval 
@Bean org.databene.benerator.anno class,
method
Defines a custom data generator to retrieve data from 
@Equivalence 
org.databene.benerator.anno
class,
method
 
Advises Feed4JUnit to use the EquivalenceGeneratorFactory for all related test methods. It creates relatively small numbers of mean tests using mechanisms from border value and partition testing. 
@Coverage 
org.databene.benerator.anno
class,
method
 
Advises Feed4JUnit to use the CoverageGeneratorFactory for all related test methods. It runs through all possible values and all possible combinations and produces a large number of tests.
@Stochastic org.databene.benerator.anno
class,
method
 
Advises Feed4JUnit to use the StochasticGeneratorFactory for all related test methods. It creates gentle datarandomly and provides for an unlimited number of tests.

 

 

 

Smoke Testing 

Performing smoke tests with random but valid data can strongly improve code coverage and exhibit bugs from very special data constellations. Even if you cannot simply predict each result of random data, you can check result constraints or at least check for runtime exceptions.

In FeedJUnit, you can use the @Stoachstic annotation to generate random data or the @Coverage annotation to first generate border values and then all values between. Be aware that you need to restrict the InvocationCount in many cases (unless you want to test billions of calls):

@RunWith(Feeder.class)
public class AddTest {

    @Test
    @Coverage
    @InvocationCount(100)
    public void testAdd(int param1, int param2) {
        try {
            int result = MyUtil.add(param1, param2);
        } catch (Exception e) {
            // accept application exceptions, fail on runtime exceptions
            // like NullPointerException

            
if (e instanceof RuntimeException)
                throw e;

        }  
    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics