2

I am a beginner in JUnit. I want to create a test to get all the products and to get the products by id. This is my Java code:

@Path("/produits")
@Produces("application/json")
public class ProduitResource {

    public ProduitResource() {
    }

    @GET
    public List<Produit> getProduits() {
        System.out.println("getProduits");

        return ReadXMLFile.getProduits();
    }

    @GET
    @Path("numProduit-{id}")
    public Produit getProduit(@PathParam("id") String numProduit) {
        System.out.println("getProduit");

        for (Produit current : ReadXMLFile.getProduits()) {
            if (numProduit.equals(current.getNumProduit())) {
                return current;
            }
        }
        return null;
    }

    @GET
    @Path("/search")
    public List<Produit> searchProduitsByCriteria(@QueryParam("departure") String departure, @QueryParam("arrival") String arrival, @QueryParam("arrivalhour") String arrivalHour) {
        System.out.println("searchProduitsByCriteria");

        return ReadXMLFile.getProduits().subList(0, 2);
    }
}
dur
  • 15,689
  • 25
  • 79
  • 125
CooperShelly
  • 103
  • 2
  • 6
  • 12

3 Answers3

8

Presuming that what you want is to make a unit test, as opposed to an integration, functional or another type of test, you should simply instantiate ProduitResource and run tests on it:

@Test
public void testFetchingById() {
   ProduitResource repo = new ProduitResource();
   Produit prod = repo.getProduit("prod123");
   assertNotNull(prod);
   assertEquals(prod.getId(), "prod123");
}

Doing this might require mocking the environment, in your case you might need to mock whatever the Produits are being fetched from.

If you were to actually fire HTTP requests at it, this would require a server running and would no longer constitute a unit test (as you'd be testing more than just this unit's own functionality). To do this, you could make your build tool start up the server before running tests (Jetty Maven plugin for example can be used here to start Jetty in pre-integration-test phase), or you could make JUnit do it in a preparation step (@BeforeClass) as described here. Similar logic for shutting the server down (use post-integration-test phase in Maven or @AfterClass in JUnit).

There's many libraries that help you write the actual tests for RESTful resources, rest-assured being a good one.

kaqqao
  • 12,984
  • 10
  • 64
  • 118
0

There are two basic strategies that you can use (and you may want to do both).

  1. Redesign the code for your web service so you can mock out dependencies on any object that interacts with the environment, such as ReadXMLFile in your example. The mock objects would return canned data instead of reading any file or database. Use a mocking framework such as Mockito to make the creation of mock objects less effort. Then you can instantiate the ProductResource class in your JUnit tests and call it like any other Java code.

  2. Create JUnit set up and tear down methods (using @Before, @BeforeClass, @After, and @AfterClass annotations) that will set up a test environment, seed data, and deploy your application and clean it up when the tests are over. Then use a REST client API, such as Jersey client API or Spring RestTemplate, to call your web service and get the results.

J. Lenthe
  • 1,330
  • 10
  • 10
0

As kaqqao mentioned, you can simply instantiate ProduitResource and test it but in this case you won't be able to make HTTP call, check HTTP status. REST Assured is suitable for testing REST service but problem is that you'll need to run it as separate instance which is not convenient - RestAssured testing without running Tomcat. Another option is to use Jersey Test (How to in-memory unit test Spring-Jersey) which provides ability to test REST service in-memory.

Community
  • 1
  • 1
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114