测试在抛出预期异常后的响应。

huangapple go评论87阅读模式
英文:

Testing response after expected exception is thrown

问题

使用以下测试(JUnit4),断言不会被调用。这是预期的行为吗?我期望会抛出 ResponseStatusException 异常,测试验证了这一点,但之后我希望对响应进行断言。还是因为抛出了异常,所以检查响应的内容是无效的?

@Test(expected = ResponseStatusException.class)
public void testResponse(){

    final Long ticketId = null

    when(service.getTicket(null))
            .thenThrow(new NullPointerException("ticketId cannot be null"));

    //Execute
    ResponseEntity<List<TicketResponse>> response = service.getTicket(null);

    assertEquals(HttpStatus.OK, response.getStatusCode());
}
英文:

Using below test (JUnit4) the Asserts are not invoked. Is this the expected behaviour ? I expect ResponseStatusException to be thrown, the test verifies this but then I wish to assert on the response. Or is the reasoning as an exception is thrown checking the content of the response is not valid ?

@Test(expected = ResponseStatusException.class)
public void testResponse(){

    final Long ticketId = null

    when(service.getTicket(null))
            .thenThrow(new NullPointerException(&quot;ticketId cannot be null&quot;));

    //Execute
    ResponseEntity&lt;List&lt;TicketResponse&gt;&gt; response = service.getTicket(null);

    assertEquals(HttpStatus.OK, response.getStatusCode());


}

答案1

得分: 2

是的,这是正常的,尽管请注意您将无法验证响应,因为会抛出异常,所以您不会得到响应!不过,您可以验证异常状态。

为此,您可能想阅读官方 Junit 4 文档中的“异常测试”页面(代码摘自那里),在那里您基本上使用 assertThrows 方法而不是 @Test(expected=),这允许您进行更多的验证。

另一种选择是使用 ExpectedException Rule。同样,可以查看链接获取示例。

https://github.com/junit-team/junit4/wiki/Exception-testing

@Test
public void testExceptionAndState() {
  List<Object> list = new ArrayList<>();
  
  IndexOutOfBoundsException thrown = assertThrows(
      IndexOutOfBoundsException.class,
      () -> list.add(1, new Object()));
  
  // 对抛出的异常进行断言
  assertEquals("Index: 1, Size: 0", thrown.getMessage());
  // 在异常被抛出后,对领域对象的状态进行断言
  assertTrue(list.isEmpty());
}
英文:

Yes, this is normal, though note you won't be able to verify the response anyway, as an exception is thrown so you don't get a response! You could verify the exception state however.

For that, you might want to read up on the "Exception Testing" page of the official Junit 4 documentation (code taken from there), where you basically use the assertThrows method and not @Test(expected=), which allows you to do more verifications.

Another alternative would be using the ExpectedException Rule. Again, see the link for an example.

https://github.com/junit-team/junit4/wiki/Exception-testing

@Test
public void testExceptionAndState() {
  List&lt;Object&gt; list = new ArrayList&lt;&gt;();

  IndexOutOfBoundsException thrown = assertThrows(
      IndexOutOfBoundsException.class,
      () -&gt; list.add(1, new Object()));

  // assertions on the thrown exception
  assertEquals(&quot;Index: 1, Size: 0&quot;, thrown.getMessage());
  // assertions on the state of a domain object after the exception has been thrown
  assertTrue(list.isEmpty());
}

huangapple
  • 本文由 发表于 2020年4月8日 03:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定