주니트 4의 @parameters
-
05-07-2019 - |
문제
junit 테스트 클래스에서 매개 변수화 된 클래스로 실행중인 @Parameters를 사용하여 둘 이상의 방법을 가질 수 있습니까?
@RunWith(value = Parameterized.class)
public class JunitTest6 {
private String str;
public JunitTest6(String region, String coverageKind,
String majorClass, Integer vehicleAge, BigDecimal factor) {
this.str = region;
}
@Parameters
public static Collection<Object[]> data1() {
Object[][] data = {{some data}}
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized str is : " + str);
str = null;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = {{some other data}}
return Arrays.asList(data);
}
@Test
public void pullTest() {
System.out.println("Parameterized new str is : " + str);
str = null;
}
}
해결책
당신은 사용할 수 있습니다 이론 다른 매개 변수를 다른 방법으로 전달하려면 러너 (해당 링크의 이론을 검색).
다른 팁
아마 data1
방법이지만 보장하지 않으면 JVM이 Junit4를 먼저 제공하는 사람은 먼저 사용됩니다.
Junit의 관련 코드는 다음과 같습니다.
private FrameworkMethod getParametersMethod(TestClass testClass) throws Exception {
List<FrameworkMethod> methods= testClass.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
int modifiers= each.getMethod().getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
return each;
}
throw new Exception("No public static parameters method on class " + testClass.getName());
}
따라서 찾은 최초의 공개 정적 주석이있는 방법이 사용되지만 어떤 순서로든 찾을 수 있습니다.
왜 그런 식으로 UOUR 테스트를 작성 했습니까? 당신은 하나만 있어야합니다 @Parameters
-예고 된 방법.
둘 이상의 데이터 방법을 갖도록 지정되지 않았습니다. 당신은 그것을 볼 수 있습니다 Skaffman의 대답.
두 가지 데이터 방법을 구현하기 위해 제공되지 않는 이유는 무엇입니까?
대답은 다음과 같습니다. 커플 링.
이 테스트를 두 개의 테스트 케이스로 나누기에는 너무 복잡합니까? 작은 상속을 도입하고 공통 방법을 공유 할 수 있습니다. 두 개의 테스트 케이스를 사용하면 두 개의 분리 된 데이터 방법을 제공하고 물건을 매우 잘 테스트 할 수 있습니다.
도움이되기를 바랍니다.
동일한 매개 변수에서 작동하는 각 메소드 세트에 대한 내부 클래스를 만들 수 있습니다. 예를 들어:
public class JunitTest6 {
@RunWith(value = Parameterized.class)
public static class PushTest{
private String str;
public PushTest(String region) {
this.str = region;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = {{some data}}
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized str is : " + str);
str = null;
}
}
@RunWith(value = Parameterized.class)
public static class PullTest{
private String str;
public PullTest(String region) {
this.str = region;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = {{some other data}}
return Arrays.asList(data);
}
@Test
public void pullTest() {
System.out.println("Parameterized new str is : " + str);
str = null;
}
}
}
제휴하지 않습니다 StackOverflow