2011-09-26 9 views
15
@DataPoints public static final Integer[] input1={1,2}; 
@Theory 
@Test 
public void test1(int input1){ 

} 

@DataPoints public static final Integer[] input2={3,4}; 
@Theory 
@Test 
public void test2(int input2){ 

} 

test1은 input1 - {1,2} 데이터 세트로 실행하고 test2는 input2 - {3,4}로 실행합니다. 그러나 현재 각 테스트는 두 데이터 세트 {1,2,3,4}로 실행됩니다. 특정 @DataPoints를 특정 @Theorys에 바인딩하는 방법DataPoint를 이론에 연결하는 방법은 무엇입니까?

답변

22

데이터 포인트가 클래스에 적용됩니다. int를 취하는 @Theory 메서드가 있고 int 배열 인 DataPoint가 있으면 int로 호출됩니다.

@RunWith(Theories.class) 
public class TheoryTest { 
    @DataPoint public static int input1 = 45; 
    @DataPoint public static int input2 = 46; 
    @DataPoints public static String[] inputs = new String[] { "foobar", "barbar" }; 

    @Theory public void testString1(String input) { 
     System.out.println("testString1 input=" + input); 
    } 

    @Theory public void testString2(String input) { 
     System.out.println("testString2 input=" + input); 
    } 

    @Theory public void test1(int input) { 
     System.out.println("test1 input=" + input); 
    } 

    @Theory public void test2(int input) { 
     System.out.println("test2 input=" + input); 
    } 
} 

이 45 & 46 그것은 "는 foobar"및 "barbar"와 "는 foobar"및 "barbar"와 함께 testString2 testString1 호출 45 & 46 TEST1 및 TEST2 부른다.

당신은 정말, 당신은 개인 클래스에 데이터를 래핑 수있는 다른 이론에 대해 서로 다른 데이터 세트를 사용하려면 :

@RunWith(Theories.class) 
public class TheoryTest { 
    public static class I1 { int i; public I1(int i) { this.i = i;} } 
    public static class I2 { int i; public I2(int i) { this.i = i;} } 

    @DataPoint public static I1 input1 = new I1(45); 
    @DataPoint public static I2 input2 = new I2(46); 

    @Theory 
    public void test1(I1 input) { 
     System.out.println("test1 input=" + input.i); 
    } 

    @Theory 
    public void test2(I2 input) { 
     System.out.println("test2 input=" + input.i); 
    } 
} 

이이 작품 (46)와 45 TEST1 및 TEST2가 호출하지만 내 의견은 코드를 모호하게 만들고 Test 클래스를 두 클래스로 분리하는 더 좋은 해결책 일 수 있습니다.

+0

@Mathew 답장을 보내 주셔서 감사합니다. 따라서 바인딩 기능은 없습니다. 당분간은 개인 클래스의 래핑을 사용합니다. 그러나 여전히 우아하지 않습니다. DataPoint가 클래스에 연결된 이유를 아직도 이해할 수 없습니까? 그들은 방법 수준에서 더 잘 이해합니다. –

+0

아이디어는 당신이 많은 데이터 포인트를 가지고 있고, 많은 이론을 테스트 할 수 있다는 것입니다 : 당신은 많은 테스트 방법이 필요합니다. 당신이 대답에 만족한다면, 제발 받아 들여 주시겠습니까? –

+0

+1 Test 클래스를 두 개의 클래스로 나누는 경우. test1과 test2가 서로 다른 데이터 집합을 전달하고 이해할 필요가 있다면 본질적으로 다른 동작을 테스트하고 논리적으로 분리 할 수 ​​있습니다. –

1

몇 가지 참조 필자는 동작 확인을위한 특정 값과 이론에 대한 테스트를 사용하는 방법에 대해 살펴 보았습니다. 예를 들어, 속성에서 더하거나 빼는 메소드가있는 클래스가있는 경우, 테스트는 결과의 정확성을 검증합니다 (예 : 1 + 3은 4를 반환합니다) 반면 이론은 데이터 포인트 값 (x1 , y1), (x2, y2), x + yy는 항상 x와 같고, x-y + y는 항상 x와 같고, x * y/y는 항상 x와 같습니다. 이와 같은 방법으로 이론 결과는 자료. 이론을 사용하면 y == 0과 같은 사례를 필터링 할 수도 있습니다. 실패로 간주되지 않습니다. 결론 : 둘 다 사용할 수 있습니다. 좋은 논문은 다음과 같습니다 http://web.archive.org/web/20110608210825/http://shareandenjoy.saff.net/tdd-specifications.pdf의 JUnit 4.12 (이 도입되었을 때 확실하지)

21

는 데이터 포인트의 이름을 지정하고 (내가 http://farenda.com/junit/junit-theories-with-datapoints/에서 배운) 매개 변수에 할당 할 수 있습니다 :

@RunWith(Theories.class) 
public class TheoriesAndDataPointsTest { 
    @DataPoints("a values") 
    public static int[] aValues() { 
     return new int[]{1, 2}; 
    } 

    @DataPoints("b values") 
    public static int[] bValues() { 
     return new int[]{3, 4}; 
    } 

    @Theory 
    public void theoryForA(@FromDataPoints("a values") int a) { 
     System.out.printf("TheoryForA called with a = %d\n", a); 
    } 

    @Theory 
    public void theoryForB(@FromDataPoints("b values") int a) { 
     System.out.printf("TheoryForB called with b = %d\n", a); 
    } 
} 

출력 :

TheoryForA called with a = 1 
TheoryForA called with a = 2 
TheoryForB called with b = 3 
TheoryForB called with b = 4 
+1

이것은 허용 된 대답이어야합니다. – Chris311

+0

@ Chris311, 최신 JUnit을 사용할 수있는 사용자 만 가능합니다. 불행하게도 우리는 이전 버전의 IDE를 고수하고, 적어도 특정 시점까지 Eclipse를 지원합니다. JUnit 라이브러리를 미리 패키지화하여 사용하는 것이 좋습니다. –

관련 문제