ProductHandler { public void setProductModules(final String name, final List<String> moduleNames) { final DbProduct dbProduct = getProduct(name); dbProduct.setModules(moduleNames); repositoryHandler.store(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 setProductModules(){ 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 List<String> moduleNames = Lists.newArrayList("module1", "module2", "module3"); productHandler.setProductModules(product.getName(), moduleNames); final ArgumentCaptor<DbProduct> captor = ArgumentCaptor.forClass(DbProduct.class); verify(repositoryHandler, times(1)).store(captor.capture()); assertEquals(moduleNames, captor.getValue().getModules()); }
@Test public void setProductModulesForAProductThatDoesNotExist(){ final RepositoryHandler repositoryHandler = GrapesTestUtils.getRepoHandlerMock(); final ProductHandler productHandler = new ProductHandler(repositoryHandler); Exception exception = null; try { final List<String> moduleNames = Lists.newArrayList("module1", "module2", "module3"); productHandler.setProductModules("doesNotExist", moduleNames); } catch(Exception e){ exception = e; } assertNotNull(exception); }