2017-10-25 14 views
1

특정 메서드가 예외없이 문자열 묶음을 처리 할 수 ​​있는지 테스트하고 싶습니다. 따라서, 나는 AssertJ의 soft assertions를 사용하고자하는, 뭔가 같은 : 불행하게도AssertJ 1.x 소프트 어서션을 사용하여이 메서드가 예외를 throw하지 않는다고 가정합니다.

SoftAssertion softly = new SoftAssertion(); 

for (String s : strings) { 
    Foo foo = new Foo(); 

    try { 
     foo.bar(s); 
     // Mark soft assertion passed. 
    } catch (IOException e) { 
     // Mark soft assertion failed. 
    } 
} 

softly.assertAll(); 

, 나는 자바 6 각각 AssertJ 1.x에서에 충실해야한다, 그래서 나는이 활용할 수 없습니다

assertThatCode(() -> { 
    // code that should throw an exception 
    ... 
}).doesNotThrowAnyException(); 

AssertJ (또는 JUnit)에서이 작업을 수행하는 방법이 있습니까?

답변

4

테스트 코드에 루프가있는 것은 좋지 않습니다.

테스트 내에서 실행되는 코드가 예외를 throw하면 테스트가 실패합니다. JUnit 용 매개 변수화 된 러너를 사용하는 것이 좋습니다 (라이브러리와 함께 제공됨). 공식 JUnit 4에서는 문서에서

예 : 나는 투표까지 것 대신에 링크를 예를 게시 할 경우

import static org.junit.Assert.assertEquals; 
import java.util.Arrays; 
import java.util.Collection; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 
import org.junit.runners.Parameterized.Parameters; 

@RunWith(Parameterized.class) 
public class FibonacciTest { 
    @Parameters 
    public static Collection<Object[]> data() { 
     return Arrays.asList(new Object[][] {  
       { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } 
      }); 
    } 

    private int fInput; 

    private int fExpected; 

    public FibonacciTest(int input, int expected) { 
     fInput= input; 
     fExpected= expected; 
    } 

    @Test 
    public void test() { 
     // Basically any code can be executed here 
     assertEquals(fExpected, Fibonacci.compute(fInput)); 
    } 
} 

public class Fibonacci { 
    public static int compute(int n) { 
     int result = 0; 

     if (n <= 1) { 
      result = n; 
     } else { 
      result = compute(n - 1) + compute(n - 2); 
     } 

     return result; 
    } 
} 
+0

, 대답은 좋지만, 시간에 링크가 중단되고 더 독자를 할 수 없습니다 다음을 참조하십시오 –

+0

좋은 지적, 감사합니다! – Admit

+0

이제 완벽한 대답입니다, 훌륭한 직업, 지식 공유에 감사드립니다. –