I have implemented some simple web services, and I want to test them using unit tests. I really don't know how to do it for the POST
and DELETE
methods. Also I would like to use the @Before
and After
annotations.
Here is my code:
public class Server {
public static void main(String[] args) {
JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
serverFactory.setResourceClasses(SubscriptionService.class);
serverFactory.setResourceProvider(SubscriptionService.class,
new SingletonResourceProvider(new SubscriptionService()));
serverFactory.setAddress("http://localhost:8080/service/");
serverFactory.setProvider(JacksonJsonProvider.class);
serverFactory.create();
}
}
the class for the Service
@Path("/")
public class SubscriptionService {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/status/{subsId}")
public Response getSubscriptionStatus(@PathParam("subsId") int subsId) {
return Response.status(200).entity("subscription status for id: " + subsId).build();
}
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public String subscribe(String url) {
// subscriberList.add(url);
return "subscribed";
}
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/subscriber/{subsId}")
public String unsubscribe(@PathParam("subId") int subsId ) {
// subscriberList.remove(subsId);
return "unsubscribed";
}
}
and the test class:
public class TestSubscriptionService {
private SubscriptionService service = new SubscriptionService();
@Test
public void testGetSubscriptionStatus() {
final Response response = service.getSubscriptionStatus(5);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("subscription status for id: 5", response.getEntity());
}
@Test
public void testSubscribe() {
}
}
The test for the GET
method passes but I don't know if it is ok like I implemented it.
Can anyone help me with some advices
Aucun commentaire:
Enregistrer un commentaire