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.