ProductHandler { public DbProduct getProduct(final String name) { final DbProduct dbProduct = repositoryHandler.getProduct(name); if(dbProduct == null){ throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND) .entity("Product " + name + " does not exist.").build()); } return dbProduct; } ProductHandler(final RepositoryHandler repositoryHandler); void create(final DbProduct dbProduct); void update(final DbProduct dbProduct); List<String> getProductNames(); DbProduct getProduct(final String name); void deleteProduct(final String name); void setProductModules(final String name, final List<String> moduleNames);  }
@Test public void getAnExistingProduct(){ final DbProduct product = new DbProduct(); product.setName("product1"); final RepositoryHandler repositoryHandler = GrapesTestUtils.getRepoHandlerMock(); when(repositoryHandler.getProduct(product.getName())).thenReturn(product); final ProductHandler productHandler = new ProductHandler(repositoryHandler); final DbProduct gotProduct = productHandler.getProduct(product.getName()); assertNotNull(gotProduct); assertEquals(product, gotProduct); }
@Test public void getAProductThatDoesNotExist(){ final RepositoryHandler repositoryHandler = GrapesTestUtils.getRepoHandlerMock(); final ProductHandler productHandler = new ProductHandler(repositoryHandler); Exception exception = null; try { productHandler.getProduct("doesNotExist"); } catch(Exception e){ exception = e; } assertNotNull(exception); }