简体   繁体   English

Ploeh的AutoFixture for .Net是否有Java替代方案?

[英]Is there a Java alternative to Ploeh's AutoFixture for .Net?

I am looking for a Java tool that would create anonymous variables (variables whose value I don't care about) in my tests, similar to AutoFixture in .Net. 我正在寻找一个Java工具,可以在我的测试中创建匿名变量 (我不关心它的值的变量),类似于.Net中的AutoFixture。 Here is a link to AutoFixture's readme , which has pretty good examples of what it does. 这是AutoFixture自述文件的链接,它有很好的例子。

Here is a short example taken from the same readme: 以下是同一自述文件中的简短示例:

[TestMethod]
public void IntroductoryTest()
{
    // Fixture setup
    Fixture fixture = new Fixture();

    int expectedNumber = fixture.CreateAnonymous<int>();
    MyClass sut = fixture.CreateAnonymous<MyClass>();
    // Exercise system
    int result = sut.Echo(expectedNumber);
    // Verify outcome
    Assert.AreEqual<int>(expectedNumber, result, "Echo");
    // Teardown
}

Is there such a tool in the Java world? Java世界中有这样的工具吗?

Edit: 编辑:

I tried QuickCheck and while it managed to do something like what I was looking for: 我尝试过QuickCheck,虽然它设法做了我想要的事情:

import net.java.quickcheck.Generator;
import net.java.quickcheck.generator.PrimitiveGenerators;
import net.java.quickcheck.generator.support.ObjectGeneratorImpl;

public class Main {

interface Test{
     String getTestValue();
}

public static void main(String[] args) {
    Generator<String> stringGen = PrimitiveGenerators.strings(5, 100);
    Generator<Integer> intGen = PrimitiveGenerators.integers(5, 20);

    ObjectGeneratorImpl<Test> g = new ObjectGeneratorImpl<>(Test.class);
    g.on(g.getRecorder().getTestValue()).returns(stringGen);


    for (int i = 0; i < intGen.next(); i++) {
        System.out.println("value of testValue is: " + g.next().getTestValue());
    }
}

}

The tool seems to work only with interfaces. 该工具似乎只适用于接口。 If I change Test to be a class and the method to a field, the generator throws an exception that only interfaces are supported. 如果我将Test更改为类,并将方法更改为字段,则生成器会抛出仅支持接口的异常。

I sincerely hope that there is something better, especially since the documentation is seriously lacking. 我真诚地希望有更好的东西,特别是因为文件严重缺乏。

There's also JFixture which is available on github and published to maven central . 还有JFixture可以在github上找到并发布到maven central

This is still under active development, and feature requests are being honoured. 这仍处于积极开发阶段,功能请求正在得到尊重。

Ivan, 伊万,

I started a project focused on reimplementing core features of AutoFixture in java . 我开始了一个专注于重新实现java中AutoFixture核心功能的项目 AutoFixture has certainly a lot of features, so I need to prioritize which ones to implement first and which ones not to bother implementing at all. AutoFixture肯定有很多功能,所以我需要优先考虑首先实现哪些功能,哪些功能根本不需要执行。 As the project is just started, I welcome testing, defect reports and feature requests. 由于项目刚刚开始,我欢迎测试,缺陷报告和功能请求。

Try object factory. 试试对象工厂。 It is open sourced on github . 它是在github上开源的。 It can create random Java objects in just a single line of code. 它可以在一行代码中创建随机Java对象。 And it is highly configurable. 它具有高度可配置性。

Example: 例:

ObjectFactory rof = new ReflectionObjectFactory();

String str = rof.create(String.class);
Customer cus = rof.create(Customer.class);

It is also available in Maven Central Repository. 它也可以在Maven Central Repository中找到。

There is a Java implementation of QuickCheck, which has APIs for generating test data: 有一个QuickCheck的Java实现,它有用于生成测试数据的API:

http://java.net/projects/quickcheck/pages/Home http://java.net/projects/quickcheck/pages/Home

I'm not too familiar with AutoFixture, and I suspect that QuickCheck is a slightly different kind of test framework, but maybe it is useful for solving your specific problem. 我对AutoFixture并不太熟悉,我怀疑QuickCheck是一种稍微不同的测试框架,但它可能对解决您的特定问题很有用。

ObjectGenerator is more of an experimental feature: ObjectGenerator更像是一个实验性功能:

ObjectGenerator<Test> objects = PrimitiveGenerators.objects(Test.class);
objects.on(objects.getRecorder().getTestValue()).returns(PrimitiveGenerators.strings());

Test next = objects.next();
System.out.println(next.getTestValue());

I'd prefer a simple Generator implementation: 我更喜欢简单的Generator实现:

class TestGenerator implements Generator<Test>{
    Generator<String> values = PrimitiveGenerators.strings();
    @Override public Test next() {
        return new TestImpl(values.next());
    }   
}

I am using JFixture along Mockito .spy() for that ;) 我在Mockito .spy()中使用JFixture ;)

Let's see an example how to do something that it would be trivial with AutoFixture and C#. 让我们看一个如何使用AutoFixture和C#做一些简单的事情的例子。 The idea here is to generate random data in your object except for some specific methods that need to have specific values. 这里的想法是在对象中生成随机数据,除了一些需要具有特定值的特定方法。 It is interesting I didn't find that somewhere stated.. This technique eliminates the "Arrange" part of your unit tests to be a small number of lines and in addition focuses on what values need to be specific for this unit test to pass 有趣的是,我没有找到某处说明的内容。这种技术消除了单元测试中的“排列”部分是少量的行,另外还关注需要特定的值才能使这个单元测试通过

public class SomeClass {
    public int id; //field I care
    public String name; // fields I don't care
    public String description; //fields I don't care

    public int getId(){
        return id;
    } 

    public void setId(int id){
        this.id = id;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getDescription(){
        return description;
    }

    public void setDescirption(String description){
        this.description = description;
    }
}



public static void main(String args[]){
    JFixture fixture = new JFixture();
    fixture.customise().circularDependencyBehaviour().omitSpecimen(); //omit circular dependencies
    fixture.customise().noResolutionBehaviour().omitSpecimen(); // omit methods that cannot be resolved
    SomeClass entity = fixture.create(SomeClass.class);
    SomeClass mock = Mockito.spy(entity);
    Mockito.when(mock.getId()).thenReturn(3213);

    System.out.println(mock.getId()); // always 3213
    System.out.println(mock.getName()); // random
    System.out.println(mock.getDescription()); //random
}

This prints: 这打印:

3213
name9a800265-d8ef-4be9-bd45-f0b62f791d9c
descriptiona9f9245f-eba1-4805-89e3-308ef69e7091

Yet Another QuickCheck for Java is another tool you may probably take a look. 另一个QuickCheck for Java是另一个你可能会看一看的工具。

It is very integrated with JUnit (it supports tests with parameters, annotations to configure the generated objects and so on). 它与JUnit非常集成(它支持带参数的测试,注释来配置生成的对象等)。

It has a lot of generators (all of quickcheck, and some specific to OOP, such as interfaces, abstract classes and singleton generators), and you can define your own ones. 它有很多生成器(所有的快速检查,一些特定于OOP,如接口,抽象类和单例生成器),你可以定义自己的生成器。 There is also a constructor-based generator. 还有一个基于构造函数的生成器。

Currently is in alpha status, but if you take a look to the downloads page you'll see a basic documentation. 目前处于alpha状态,但如果您查看下载页面,您将看到基本文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM