@ -31,10 +31,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.bind.annotation.RestController ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
import static org.hamcrest.Matchers.containsString ;
import static org.hamcrest.Matchers.containsString ;
/ * *
/ * *
* Samples of tests using { @link WebTestClient } with serialized JSON content .
* Samples of tests using { @link WebTestClient } with serialized JSON content .
*
*
@ -49,13 +49,32 @@ public class JsonContentTests {
@Test
@Test
public void jsonContent ( ) {
public void jsonContent ( ) {
this . client . get ( ) . uri ( "/persons" )
this . client . get ( ) . uri ( "/persons/extended " )
. accept ( MediaType . APPLICATION_JSON )
. accept ( MediaType . APPLICATION_JSON )
. exchange ( )
. exchange ( )
. expectStatus ( ) . isOk ( )
. expectStatus ( ) . isOk ( )
. expectBody ( ) . json ( "[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]" ) ;
. expectBody ( ) . json ( "[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]" ) ;
}
}
@Test
public void jsonContentStrictFail ( ) {
assertThatExceptionOfType ( AssertionError . class ) . isThrownBy ( ( ) - > this . client . get ( ) . uri ( "/persons/extended" )
. accept ( MediaType . APPLICATION_JSON )
. exchange ( )
. expectStatus ( ) . isOk ( )
. expectBody ( ) . json ( "[{\"name\":\"Jane\"},{\"name\":\"Jason\"},{\"name\":\"John\"}]" , true )
) ;
}
@Test
public void jsonContentStrict ( ) {
this . client . get ( ) . uri ( "/persons/extended" )
. accept ( MediaType . APPLICATION_JSON )
. exchange ( )
. expectStatus ( ) . isOk ( )
. expectBody ( ) . json ( "[{\"name\":\"Jane\",\"surname\":\"Williams\"},{\"name\":\"Jason\",\"surname\":\"Johnson\"},{\"name\":\"John\",\"surname\":\"Smith\"}]" , true ) ;
}
@Test
@Test
public void jsonPathIsEqualTo ( ) {
public void jsonPathIsEqualTo ( ) {
this . client . get ( ) . uri ( "/persons" )
this . client . get ( ) . uri ( "/persons" )
@ -98,6 +117,11 @@ public class JsonContentTests {
return Flux . just ( new Person ( "Jane" ) , new Person ( "Jason" ) , new Person ( "John" ) ) ;
return Flux . just ( new Person ( "Jane" ) , new Person ( "Jason" ) , new Person ( "John" ) ) ;
}
}
@GetMapping ( "/extended" )
Flux < ExtendedPerson > getExtendedPersons ( ) {
return Flux . just ( new ExtendedPerson ( "Jane" , "Williams" ) , new ExtendedPerson ( "Jason" , "Johnson" ) , new ExtendedPerson ( "John" , "Smith" ) ) ;
}
@GetMapping ( "/{name}" )
@GetMapping ( "/{name}" )
Person getPerson ( @PathVariable String name ) {
Person getPerson ( @PathVariable String name ) {
return new Person ( name ) ;
return new Person ( name ) ;
@ -109,4 +133,22 @@ public class JsonContentTests {
}
}
}
}
static class ExtendedPerson {
private String name ;
private String surname ;
public ExtendedPerson ( String name , String surname ) {
this . name = name ;
this . surname = surname ;
}
public String getName ( ) {
return name ;
}
public String getSurname ( ) {
return surname ;
}
}
}
}