문제

나는 몇 가지 Junit 테스트를 작성했습니다 @Test 주석. 내 테스트 방법이 확인 된 예외를 던지고 예외와 함께 메시지를 주장하고 싶다면 Junit과 함께 할 수있는 방법이 있습니까? @Test 주석? Afaik, Junit 4.7은이 기능을 제공하지 않지만 향후 버전이 제공합니까? .NET에서 메시지와 예외 클래스를 주장 할 수 있다는 것을 알고 있습니다. Java 세계에서 비슷한 기능을 찾고 있습니다.

이것이 내가 원하는거야:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}
도움이 되었습니까?

해결책

당신은 사용할 수 있습니다 @Rule 주석 ExpectedException, 이와 같이:

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Employee ID is null");

    // do something that should throw the exception...
    System.out.println("=======Starting Exception process=======");
    throw new NullPointerException("Employee ID is null");
}

예제입니다 ExpectedException 문서는 (현재) 잘못되었습니다 - 공개 생성자가 없으므로 사용해야합니다. ExpectedException.none().

다른 팁

나는 좋아한다 @Rule 대답. 그러나 어떤 이유로 든 규칙을 사용하고 싶지 않습니다. 세 번째 옵션이 있습니다.

@Test (expected = RuntimeException.class)
public void myTestMethod()
{
   try
   {
      //Run exception throwing operation here
   }
   catch(RuntimeException re)
   {
      String message = "Employee ID is null";
      assertEquals(message, re.getMessage());
      throw re;
    }
    fail("Employee Id Null exception did not throw!");
  }

사용해야합니까? @Test(expected=SomeException.class)? 우리가 예외의 실제 메시지를 주장해야 할 때, 이것이 우리가하는 일입니다.

@Test
public void myTestMethod()
{
  try
  {
    final Integer employeeId = null;
    new Employee(employeeId);
    fail("Should have thrown SomeException but did not!");
  }
  catch( final SomeException e )
  {
    final String msg = "Employee ID is null";
    assertEquals(msg, e.getMessage());
  }
}

Junit 4.13 (일단 출시)에서는 다음을 수행 할 수 있습니다.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

...

@Test
void exceptionTesting() {
  IllegalArgumentException exception = assertThrows(
    IllegalArgumentException.class, 
    () -> { throw new IllegalArgumentException("a message"); }
  );

  assertEquals("a message", exception.getMessage());
}

이것은 또한 작동합니다 주니 5 그러나 다른 수입으로 :

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

...

실제로 최상의 사용법은 시도/캐치입니다. 왜요? 예외를 기대하는 장소를 제어 할 수 있기 때문입니다.

이 예를 고려하십시오 :

@Test (expected = RuntimeException.class)
public void someTest() {
   // test preparation
   // actual test
}

언젠가 코드가 수정되고 테스트 준비가 runtimeexception을 던지면 어떻게됩니까? 이 경우 실제 테스트는 테스트되지 않으며 예외를 던지지 않더라도 테스트가 통과됩니다.

그렇기 때문에 주석에 의존하는 것보다 시도/캐치를 사용하는 것이 훨씬 낫습니다.

Raystorm은 좋은 대답을했습니다. 나도 규칙을 좋아하는 팬이 아닙니다. 나는 처음부터 큰 주석 중 하나 인 가독성과 유용성을 돕기 위해 다음 유틸리티 클래스를 만듭니다.

이 유틸리티 클래스 추가 :

import org.junit.Assert;

public abstract class ExpectedRuntimeExceptionAsserter {

    private String expectedExceptionMessage;

    public ExpectedRuntimeExceptionAsserter(String expectedExceptionMessage) {
        this.expectedExceptionMessage = expectedExceptionMessage;
    }

    public final void run(){
        try{
            expectException();
            Assert.fail(String.format("Expected a RuntimeException '%s'", expectedExceptionMessage));
        } catch (RuntimeException e){
            Assert.assertEquals("RuntimeException caught, but unexpected message", expectedExceptionMessage, e.getMessage());
        }
    }

    protected abstract void expectException();

}

그런 다음 내 단위 테스트의 경우이 코드 만 있으면됩니다.

@Test
public void verifyAnonymousUserCantAccessPrivilegedResourceTest(){
    new ExpectedRuntimeExceptionAsserter("anonymous user can't access privileged resource"){
        @Override
        protected void expectException() {
            throw new RuntimeException("anonymous user can't access privileged resource");
        }
    }.run(); //passes test; expected exception is caught, and this @Test returns normally as "Passed"
}

@Rule을 사용하는 경우 예외 세트는 테스트 클래스의 모든 테스트 방법에 적용됩니다.

나는 user64141의 답변을 좋아하지만 더 일반화 될 수 있음을 발견했습니다. 내 테이크는 다음과 같습니다.

public abstract class ExpectedThrowableAsserter implements Runnable {

    private final Class<? extends Throwable> throwableClass;
    private final String expectedExceptionMessage;

    protected ExpectedThrowableAsserter(Class<? extends Throwable> throwableClass, String expectedExceptionMessage) {
        this.throwableClass = throwableClass;
        this.expectedExceptionMessage = expectedExceptionMessage;
    }

    public final void run() {
        try {
            expectException();
        } catch (Throwable e) {
            assertTrue(String.format("Caught unexpected %s", e.getClass().getSimpleName()), throwableClass.isInstance(e));
            assertEquals(String.format("%s caught, but unexpected message", throwableClass.getSimpleName()), expectedExceptionMessage, e.getMessage());
            return;
        }
        fail(String.format("Expected %s, but no exception was thrown.", throwableClass.getSimpleName()));
    }

    protected abstract void expectException();

}

시도 블록 내에 "실패"명령문을 남기면 관련 어설 션 예외가 잡히게됩니다. 캐치 문에서 리턴을 사용하면이를 방지합니다.

가져 오기 캐치 노출 라이브러리를 사용하십시오. 그것은보다 훨씬 깨끗합니다 ExpectedException 규칙 또는 a try-catch.

예제는 그들의 문서를 형성합니다.

import static com.googlecode.catchexception.CatchException.*;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;

// given: an empty list
List myList = new ArrayList();

// when: we try to get the first element of the list
catchException(myList).get(1);

// then: we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
assertThat(caughtException(),
  allOf(
    instanceOf(IndexOutOfBoundsException.class),
    hasMessage("Index: 1, Size: 0"),
    hasNoCause()
  )
);
@Test (expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "This is not allowed")
public void testInvalidValidation() throws Exception{
     //test code
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top