com.github.nylle.javafixture.annotations.testcases.ReflectedTestCase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javafixture Show documentation
Show all versions of javafixture Show documentation
JavaFixture is the attempt to bring Mark Seemann's AutoFixture for .NET to the Java world. Its purpose
is to generate full object graphs for use in test suites with a fluent API for customising the test objects
during generation.
package com.github.nylle.javafixture.annotations.testcases;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.stream;
public class ReflectedTestCase {
private Map, List>> matrix = new HashMap<>();
public ReflectedTestCase(TestCase testCase) {
stream(TestCase.class.getDeclaredMethods())
.sorted(Comparator.comparing(Method::getName))
.forEachOrdered(m -> matrix.compute(m.getReturnType(), (k, v) -> addTo(v, invoke(m, testCase))));
}
@SuppressWarnings("unchecked")
public T getTestCaseValueFor(Class type, int i) {
return (T) matrix.get(type).get(i);
}
@SuppressWarnings("unchecked")
private T invoke(Method method, TestCase testCase) {
try {
return (T) method.invoke(testCase);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new TestCaseException(String.format("Unable to read test-case value for '%s'", method.getName()), e);
}
}
private List addTo(List list, T value) {
if(list == null) {
list = new ArrayList<>();
}
list.add(value);
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy