Sto cercando uno strumento Java che creerebbe anonymous variables (variabili di cui non mi interessa il valore) nei miei test, simile a AutoFixture in .Net. Ecco un collegamento a AutoFixture's readme, che ha esempi abbastanza buoni di ciò che fa.Esiste un'alternativa Java a AutoFixture di Ploeh per .Net?
Ecco un breve esempio tratto dalla medesima 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
}
C'è un tale strumento nel mondo Java?
Edit:
ho provato QuickCheck e mentre è riuscito a fare qualcosa di simile a quello che stavo cercando:
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());
}
}
}
Lo strumento sembra funzionare solo con le interfacce. Se cambio Test come classe e metodo su un campo, il generatore genera un'eccezione che supporta solo le interfacce.
Spero sinceramente che ci sia qualcosa di meglio, soprattutto perché la documentazione è seriamente carente.
Ho sempre un grande sorriso quando trovo la mia domanda esatta su SO. (So che il commento non aggiunge molto valore ...) –