@ -53,6 +53,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.bind.annotation.SessionAttributes ;
import org.springframework.web.bind.annotation.SessionAttributes ;
import org.springframework.web.context.WebApplicationContext ;
import org.springframework.web.context.WebApplicationContext ;
import org.springframework.web.server.ResponseStatusException ;
import org.springframework.web.servlet.DispatcherServlet ;
import org.springframework.web.servlet.DispatcherServlet ;
import org.springframework.web.servlet.HandlerInterceptor ;
import org.springframework.web.servlet.HandlerInterceptor ;
import org.springframework.web.servlet.config.annotation.EnableWebMvc ;
import org.springframework.web.servlet.config.annotation.EnableWebMvc ;
@ -278,40 +279,59 @@ public class MockMvcTesterIntegrationTests {
class ExceptionTests {
class ExceptionTests {
@Test
@Test
void doesNotHave UnresolvedException( ) {
void hasFailedWith UnresolvedException( ) {
assertThat ( mvc . get ( ) . uri ( "/greet " ) ) . doesNotHaveUnresolvedException ( ) ;
assertThat ( mvc . get ( ) . uri ( "/error/1 " ) ) . hasFailed ( ) ;
}
}
@Test
@Test
void hasUnr esolvedException ( ) {
void hasFailedWithR esolvedException ( ) {
assertThat ( mvc . get ( ) . uri ( "/error/1 " ) ) . hasUnresolvedException ( ) ;
assertThat ( mvc . get ( ) . uri ( "/error/2 " ) ) . hasFailed ( ) . hasStatus ( HttpStatus . PAYMENT_REQUIRED ) ;
}
}
@Test
@Test
void doesNotHaveUnresolvedExceptionWithUnresolvedException ( ) {
void doesNotHaveFailedWithoutException ( ) {
assertThat ( mvc . get ( ) . uri ( "/greet" ) ) . doesNotHaveFailed ( ) ;
}
@Test
void doesNotHaveFailedWithUnresolvedException ( ) {
assertThatExceptionOfType ( AssertionError . class )
assertThatExceptionOfType ( AssertionError . class )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/error/1" ) ) . doesNotHaveUnresolvedException ( ) )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/error/1" ) ) . doesNotHaveFailed ( ) )
. withMessage ( "Expected request to succeed, but it failed" ) ;
. withMessage ( "Expected request to succeed, but it failed" ) ;
}
}
@Test
@Test
void hasUnresolvedExceptionWithoutUnr esolvedException( ) {
void doesNotHaveFailedWithR esolvedException( ) {
assertThatExceptionOfType ( AssertionError . class )
assertThatExceptionOfType ( AssertionError . class )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/greet" ) ) . hasUnresolvedException ( ) )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/error/2" ) ) . doesNotHaveFailed ( ) )
. withMessage ( "Expected request to succeed, but it failed" ) ;
}
@Test
void hasFailedWithoutException ( ) {
assertThatExceptionOfType ( AssertionError . class )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/greet" ) ) . hasFailed ( ) )
. withMessage ( "Expected request to fail, but it succeeded" ) ;
. withMessage ( "Expected request to fail, but it succeeded" ) ;
}
}
@Test
@Test
void unresolvedExceptionWithFailedRequest ( ) {
void fail ureWithU nresolvedException( ) {
assertThat ( mvc . get ( ) . uri ( "/error/1" ) ) . unresolvedException ( )
assertThat ( mvc . get ( ) . uri ( "/error/1" ) ) . fail ure( )
. isInstanceOf ( ServletException . class )
. isInstanceOf ( ServletException . class )
. cause ( ) . isInstanceOf ( IllegalStateException . class ) . hasMessage ( "Expected" ) ;
. cause ( ) . isInstanceOf ( IllegalStateException . class ) . hasMessage ( "Expected" ) ;
}
}
@Test
@Test
void unresolvedExceptionWithSuccessfulRequest ( ) {
void failureWithResolvedException ( ) {
assertThat ( mvc . get ( ) . uri ( "/error/2" ) ) . failure ( )
. isInstanceOfSatisfying ( ResponseStatusException . class , ex - >
assertThat ( ex . getStatusCode ( ) ) . isEqualTo ( HttpStatus . PAYMENT_REQUIRED ) ) ;
}
@Test
void failureWithoutException ( ) {
assertThatExceptionOfType ( AssertionError . class )
assertThatExceptionOfType ( AssertionError . class )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/greet" ) ) . unresolvedException ( ) )
. isThrownBy ( ( ) - > assertThat ( mvc . get ( ) . uri ( "/greet" ) ) . fail ure( ) )
. withMessage ( "Expected request to fail, but it succeeded" ) ;
. withMessage ( "Expected request to fail, but it succeeded" ) ;
}
}
@ -524,6 +544,11 @@ public class MockMvcTesterIntegrationTests {
throw new IllegalStateException ( "Expected" ) ;
throw new IllegalStateException ( "Expected" ) ;
}
}
@GetMapping ( "/error/2" )
public String two ( ) {
throw new ResponseStatusException ( HttpStatus . PAYMENT_REQUIRED ) ;
}
@GetMapping ( "/error/validation/{id}" )
@GetMapping ( "/error/validation/{id}" )
public String validation ( @PathVariable @Size ( max = 4 ) String id ) {
public String validation ( @PathVariable @Size ( max = 4 ) String id ) {
return "Hello " + id ;
return "Hello " + id ;