Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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. Here is a link to AutoFixture's readme, which has pretty good examples of what it does.

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?

Edit:

I tried QuickCheck and while it managed to do something like what I was looking for:

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.

I sincerely hope that there is something better, especially since the documentation is seriously lacking.

like image 632
Ivan Alagenchev Avatar asked Jul 06 '12 17:07

Ivan Alagenchev


People also ask

What is AutoFixture xUnit?

AutoFixture is a library that you can use alongside your testing framework to reduce the amount of boilerplate test code you need to write and thus improve your productivity. At its core, AutoFixture helps you setup your tests by generating anonymous test data for you.

What is JFixture?

JFixture is a Java library to assist in the writing of Unit Tests, particularly when following Test Driven Development. It generates types based on the concept of 'constrained non-determinism', which is an implementation of the Generated Value xUnit test pattern.

What is AutoFixture class?

AutoFixture is an open source library for . NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability.


2 Answers

There's also JFixture which is available on github and published to maven central.

This is still under active development, and feature requests are being honoured.

like image 187
Jane Nicholson Avatar answered Oct 06 '22 01:10

Jane Nicholson


Ivan,

I started a project focused on reimplementing core features of AutoFixture in java. 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. As the project is just started, I welcome testing, defect reports and feature requests.

like image 35
Grzesiek Galezowski Avatar answered Oct 05 '22 23:10

Grzesiek Galezowski