@POST
@Consumes( { "application/json", "application/xml" } )
@Produces( { "application/json", "application/xml" } )
public Response create( Language language )
{
try
{
language = languageBo.create( language );
}
{
throw new WebApplicationException( Response.Status.INTERNAL_SERVER_ERROR );
}
URI createdUri = URI.create( "/myapp/v1/language/" + language.getId() );
return Response.created( createdUri ).entity( language ).build();
}
given()
.param( "name", equalTo( "Zzyzzx" ) )
.param( "code", equalTo( "zz" ) )
.expect()
.body ( "name", equalTo( "Zzyzzx" ) )
.body ( "code", equalTo( "zz" ) )
.when() .post ( "/language" );
@Test
public void testCreate()
{
// given().contentType( ContentType.JSON )
given().contentType( "application/json" )
.param( "name", equalTo( "Zzyzzx" ) )
.param( "code", equalTo( "zz" ) )
.expect()//.rootPath( "" )
.body ( "name", equalTo( "Zzyzzx" ) )
.body ( "code", equalTo( "zz" ) )
.when() .post ( "/language" );
}
@Test void testRead_byId()
{
expect().body( "id", equalTo( "35" ) )
.body( "name", equalTo( "English" ) )
.body( "code", equalTo( "en" ) )
.when() .get ( "language/35" );
}
@TestNow, as far as whether I should be asking these questions, note that my ReST service worked (and works) perfectly: I am only looking tentatively to REST Assured as a means to obtain greater code-path test coverage before giving up and telling my management and down-streamers that "it is what it is". Some on the team see REST Assured as superfluous and unnecessary. I like the concept and see REST Assured tests as a statement of what is correct and encompassing usage of our service. I've been laboring under the assumption that I just don't know how to use REST Assured and that, fixing this, I'll be happy with the result and that next time I'll write to REST Assured before implementing my service in the first place.
public void testCreate()
{
Language language = new Language();
language.setName( "Zzyzzx" );
language.setCode( "zz" );
given().contentType( "application/json" )
.and()
.body( language )
.when().post( "/language" );
}
@Entity
public class Thing
{
@Id (etc.)
Integer id;
int x, y;
...
}
@Test
public void testCreate()
{
Thing thing = new Thing();
thing.x = 9;
thing.y = 11;
given().contentType( "application/json")
.and()
.body( thing )
.when() .post( "http://localhost:8000/myapp/thing" );
}
/**
* We expect that <tt>testCreate()</tt>
above added a new language, "Zzyzzx" to the
* database and that we can test deleting it.
*/
@Test
public void testDelete()
{
String json = get(
"/language/name/Zzyzzx" ).asString();
String id = JsonPath.with( json ).get(
"language.id" );
expect().statusCode( 200 )
.when() .delete(
"/language/" + id );
}