问题描述
为了避免大量冗余的测试类进行简单的集成测试,我想创建一个参数化的泛型测试类,如下例所示: @RunWith(Parameterized.class)
public class MovementTest< V extends Vehicle,T extends Track< V>> {
private final V testVehicle;
private final T testTrack;
public MovementTest(V vehicle,T track){
testVehicle = vehicle;
testTrack = track;
}
@Test
public void testMovement(){
testVehicle.moveAlong(testTrack);
}
@Parameters
public static Iterable< Object []>提供测试样例(){
Object [] [] params = {
{new Car(),new Highway()},
{new Train(),new RailRoadTrack()}
};
return Arrays.asList(params);
}
}
public interface Vehicle {
abstract void moveAlong(Track t);
}
public interface Track< E extends Vehicle> {}
public class Train implements Vehicle {
@Override
public void moveAlong(Track t){}
}
public class RailRoadTrack实现轨道<列车> {}
public class Car implements Vehicle {
@Override
public void moveAlong(Track t){}
}
public class高速公路履带车> {}
不幸的是,这个测试类是不可运行的。有没有一个简洁的方法来实现相似的东西?
#1
您可以使用 JUnit
的参数化
转轮。它的工作原理如下:
@RunWith(Parameterized.class)
public class ParametrizedTest {
private final String text;
private final int number;
public ParametrizedTest(String text,int number){
this.text = text;
this.number = number;
}
@Test
public void shouldContainNumber(){
assertTrue(text.contains(String.valueOf(number)));
}
@ Parameterized.Parameters
public static Iterable< Object []> params(){
return Arrays.asList(
new Object [] [] {
{test string 1,1},
{test string 2,2 }
}
);
}
}
您可以阅读有关此解决方案的更多信息
#2(更好)
使用 JUnitParameters
(),只需看一下:
@RunWith(JUnitParamsRunner.class)
public class JUnitParamsTest {
@Test
@Parameters
public void shouldContainNumber(String text,int number){
assertTrue(text.contains(String.valueOf(number)));
public Object [] parametersForShouldContainNumber(){
return $(
$(test string 1,1),
$(测试字符串2,2)
);
}
}
请注意,提供参数的方法名称必须适合测试名称这个解决方案似乎更好,因为(不仅)你在执行后得到更好的测试名称:
在项目网站上可以找到更好的列表:
To avoid alot of redundant test classes for simple integration testing, I'd like to create a parameterized generic test class like the following example:
@RunWith(Parameterized.class)
public class MovementTest<V extends Vehicle, T extends Track<V>> {
private final V testVehicle;
private final T testTrack;
public MovementTest(V vehicle, T track){
testVehicle = vehicle;
testTrack = track;
}
@Test
public void testMovement(){
testVehicle.moveAlong(testTrack);
}
@Parameters
public static Iterable<Object[]> provideTestExamples(){
Object[][] params = {
{ new Car(), new Highway() },
{ new Train(), new RailRoadTrack() }
};
return Arrays.asList(params);
}
}
public interface Vehicle {
abstract void moveAlong(Track t);
}
public interface Track<E extends Vehicle> { }
public class Train implements Vehicle {
@Override
public void moveAlong(Track t) {}
}
public class RailRoadTrack implements Track<Train> {}
public class Car implements Vehicle {
@Override
public void moveAlong(Track t) { }
}
public class Highway implements Track<Car> {}
Unfortunately, this test class is not runnable. Is there a concise way to implement something alike?
#1
You can use JUnit
's Parametrized
runner. It works as follows:
@RunWith(Parameterized.class)
public class ParametrizedTest {
private final String text;
private final int number;
public ParametrizedTest(String text, int number) {
this.text = text;
this.number = number;
}
@Test
public void shouldContainNumber() {
assertTrue(text.contains(String.valueOf(number)));
}
@Parameterized.Parameters
public static Iterable<Object[]> params() {
return Arrays.asList(
new Object[][]{
{"test string 1", 1},
{"test string 2", 2}
}
);
}
}
You can read more about this solution here
#2 (better)
There's also better way (I think so) using JUnitParameters
(link), just take a look:
@RunWith(JUnitParamsRunner.class)
public class JUnitParamsTest{
@Test
@Parameters
public void shouldContainNumber(String text, int number) {
assertTrue(text.contains(String.valueOf(number)));
}
public Object[] parametersForShouldContainNumber() {
return $(
$("test string 1", 1),
$("test string 2", 2)
);
}
}
Note that name of method which supplies parameters has to fit test name. This solution seems better because (not only) you get better tests names after execution:
More comprehensive list of why it's better can be found at project site:
这篇关于如何在Eclipse中运行通用的参数化JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!