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.
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.
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.
AutoFixture is an open source library for . NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With