2012-07-13 3 views
1

내 applcation을 테스트하고 내가 junit.I 내 응용 프로그램을 테스트를 시작하고는 참조가 httpd 클래스에서받은 경우 제가 테스트 할 클래스를나는 시험 세계의 안돼서 JUnit을

public class Sender implements Runnable 
{ 
    private httpd server; 

    public Sender(httpd server_) 
    { 
    this.server = server_ 
    } 

    public void run() 
    { 

    ...  

    } 
} 

null입니다. 나는 assertNotNull을 사용해야 만한다는 것을 알았지 만, SenderTest 클래스를 만든 후에 무엇을해야하는지 명확하지 않았습니다. SenderTest 클래스 (junit 프레임 워크를 통해 생성 됨) 내부에 주석이있는 메소드 @Test을 만들었습니다.하지만 그 후에해야 할 일은 무엇입니까?

+0

당신은 또한 당신의 '테스트'코드 – radimpe

+1

를 추가 할 수있는 매개 변수 귀하의 방법은 null이 아닌데, 그것은 입력 검증과 같은데, 단위 테스트는 아닙니다. 그것은 Sender 클래스의 주장 일 것입니다. 단위 테스트의 경우 Sender 클래스를 호출하여 작동하는지 확인합니다. – Thilo

답변

3

당신이 수업을 시험해야하는 것은 아닙니다.

httpd이 null인지 여부는 Sender 계약의 일부가 아니며 Sender의 클라이언트입니다.

난 당신이 다음과 같이 제안한다

  • 그것이 server_ 인수로 null를 수신하는 경우의 작동 방법을 Sender 정의합니다.

    예를 들어 이라고 말하면 좋습니다. server_null 인 경우 IllegalArgumentException이 발생합니다..

  • 지정된대로 동작하는지 테스트하는 테스트를 만듭니다. 당신이 당신의 코드를 테스트의 JUnit을 사용해야하는 경우

    try { 
        new Sender(null); 
        fail("Should not accept null argument"); 
    } catch (IllegalArgumentException expected) { 
    } 
    
+3

+1. 단위 테스트는'Sender'가 작동하는지 확인합니다. 다른 코드가'Sender'를 올바르게 사용하지 않습니다. 아마'assert' 키워드를 사용하여 입력 검증을 할 수 있습니다. – Thilo

+0

그래서 내 수업에 다른 수업과의 참조가 있다면 그 행동을 테스트 할 수 없습니까? – Mazzy

+0

물론 가능합니다. 테스트하려는 행동은 무엇입니까? – aioobe

2

같은 것을 수행하여 예를 들어,이 방법을 고려한다.

는 JUnit 테스트가 작동하는 방법의 예입니다 : 당신이 주장 할 경우


public class Factorial { 

    /** 
    * Calculates the factorial of the specified number in linear time and constant space complexity.<br> 
    * Due to integer limitations, possible calculation of factorials are for n in interval [0,12].<br> 
    * 
    * @param n the specified number 
    * @return n! 
    */ 
    public static int calc(int n) { 
     if (n < 0 || n > 12) { 
      throw new IllegalArgumentException("Factorials are defined only on non-negative integers."); 
     } 

     int result = 1; 

     if (n < 2) { 
      return result; 
     } 

     for (int i = 2; i <= n; i++) { 
      result *= i; 
     } 

     return result; 
    } 

} 

import static org.junit.Assert.*; 

import org.junit.Test; 

public class FactorialTest { 

    @Test 
    public void factorialOfZeroShouldBeOne() { 
     assertEquals(1, Factorial.calc(0)); 
    } 

    @Test 
    public void factorialOfOneShouldBeOne() { 
     assertEquals(1, Factorial.calc(1)); 
    } 

    @Test 
    public void factorialOfNShouldBeNTimesFactorialOfNMinusOne() { 
     for (int i = 2; i <= 12; i++) { 
      assertEquals(i * Factorial.calc(i - 1), Factorial.calc(i)); 
      System.out.println(i + "! = " + Factorial.calc(i)); 
     } 
    } 

    @Test(expected = IllegalArgumentException.class) 
    public void factorialOfNegativeNumberShouldThrowException() { 
     Factorial.calc(-1); 
     Factorial.calc(Integer.MIN_VALUE); 
    } 

    @Test(expected = IllegalArgumentException.class) 
    public void factorialOfNumberGreaterThanTwelveShouldThrowException() { 
     Factorial.calc(13); 
     Factorial.calc(20); 
     Factorial.calc(50); 
    } 
} 
관련 문제