如何创建一个测试类,以便注入要用于测试的类?
例如,假设我有一个接口ISort和一些实现ISort的具体类InsertionSort、MergeSort等。由于所有ISort的测试用例都是相同的,那么如何编写一个通用测试类并注入要测试的具体类呢?
这就是我到目前为止所尝试的
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SortTest {
private ISort sortingAlgorithm;
public SortTest(ISort sortingAlgorithm) {
this.sortingAlgorithm = sortingAlgorithm;
}
@Test
public void test1() {
int[] data = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] expected = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertArrayEquals(expected, sortingAlgorithm.sort(data));
}
@Test
public void test2() {
int[] data = new int[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] expected = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertArrayEquals(expected, sortingAlgorithm.sort(data));
}
// .. more tests
}如何使用InsertionSortTest类从SortTest运行测试?
class InsertionSortTest {
// ??
}发布于 2019-12-10 16:35:57
也许像这样的东西就能起到作用。
private static Stream<ISort> sortingType() {
return Stream.of(new InsertionSort(), new MergeSort());
}
@ParameterizedTest
@MethodSource("sortingType")
public void test1(ISort sortingAlgorithm) {
int[] data = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] expected = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertArrayEquals(expected, sortingAlgorithm.sort(data));
}发布于 2019-12-10 16:34:26
你试过Factory Pattern吗?让我们创建一个SortFactory
public class SortFactory {
public ISort getSortingMethod(String method) {
if(method == null) {
return null;
}
if(method.equalsIgnoreCase("InsertionSort")) {
return new InsertionSort();
}
if(method.equalsIgnoreCase("MergeSort")) {
return new MergeSort();
}
return null;
}
}然后在你的测试类中,你可以像这样使用
public class SortTest {
private ISort sortingAlgorithm;
private SortFactory sortFactory = new SortFactory();
@Test
public void test1() {
int[] data = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
sortingAlgorithm = sortFactory.getSortingMethod("MergeSort");
assertArrayEquals(expected, sortingAlgorithm.sort(data));
}
@Test
public void test2() {
int[] data = new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
sortingAlgorithm = sortFactory.getSortingMethod("InsertionSort");
assertArrayEquals(expected, sortingAlgorithm.sort(data));
}
}发布于 2019-12-10 16:47:41
可以使用@Parameterized来完成。你可能会有更多的文档,仅仅是一个开始,也是使用工厂:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class MyTest {
private static interface SortFactory {
ISort get();
}
private String someOtherParam; // example for more data
private SortFactory sortFactory;
public MyTest(SortFactory factory, String someOtherParam) {
this.sortFactory = factory;
this.someOtherParam = someOtherParam;
}
@Parameterized.Parameters(name = "{1}") // string to show in output to identify tests, here the second parameter
public static Collection<Object[]> getTestdata() {
List<Object[]> parameters = new ArrayList<Object[]>();
parameters.add(new Object[]{ new SortFactory() {
@Override
public ISort get() {
return new MergeSort();
}
}, "merge" });
parameters.add(new Object[]{ new SortFactory() {
@Override
public ISort get() {
return new InsertionSort();
}
}, "insertion" });
return parameters;
}
@Test
public void testSort() {
int[] given = new int[]{ 2, 1 };
int[] expected = new int[]{ 1, 2 };
int[] actual = sortFactory.get().sort(given);
assertArrayEquals(expected, actual);
}
}https://stackoverflow.com/questions/59262571
复制相似问题