### Question:
MenuServiceImpl implements InternalMenuService { @Override @Transactional public void createCategory(final MenuCategoryDefinition menuCategoryDefinition) { Entity menuCategory = menuCrudService.getCategory(menuCategoryDefinition); if (menuCategory != null) { return; } menuCategory = menuCrudService.createEntity(QcadooViewConstants.MODEL_CATEGORY); menuCategory.setField(MenuCategoryFields.PLUGIN_IDENTIFIER, menuCategoryDefinition.getPluginIdentifier()); menuCategory.setField(MenuCategoryFields.NAME, menuCategoryDefinition.getName()); menuCategory.setField("accessible", true); menuCategory.setField(MenuCategoryFields.SUCCESSION, menuCrudService.getTotalNumberOfCategories()); menuCategory.setField(MenuCategoryFields.AUTH_ROLE, menuCategoryDefinition.getAuthRole()); menuCrudService.save(menuCategory); }  @Override @Transactional(readOnly = true) @Monitorable MenuDefinition getMenu(final Locale locale); @Override @Transactional void addView(final MenuItemDefinition itemDefinition); @Override @Transactional void removeView(final MenuItemDefinition itemDefinition); @Override @Transactional void createCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void removeCategory(final MenuCategoryDefinition menuCategoryDefinition); @Override @Transactional void createItem(final MenuItemDefinition itemDefinition); @Override @Transactional void removeItem(final MenuItemDefinition itemDefinition);  }### Answer:
@Test public final void shouldCreateCategory() { MenuCategoryDefinition menuCategoryDefinition = new MenuCategoryDefinition(PLUGIN_IDENTIFIER, "categoryName", ROLE_VISIBLE); given(menuCrudService.getCategory(menuCategoryDefinition)).willReturn(null); given(menuCrudService.createEntity(QcadooViewConstants.MODEL_CATEGORY)).willAnswer(new Answer<Entity>() { @Override public Entity answer(final InvocationOnMock invocation) throws Throwable { DataDefinition dataDefinition = mock(DataDefinition.class); return new DefaultEntity(dataDefinition); } }); given(menuCrudService.getTotalNumberOfCategories()).willReturn(3); menuService.createCategory(menuCategoryDefinition); verify(menuCrudService).save(entityCaptor.capture()); Entity savedItem = entityCaptor.getValue(); assertEquals(PLUGIN_IDENTIFIER, savedItem.getStringField(MenuCategoryFields.PLUGIN_IDENTIFIER)); assertEquals("categoryName", savedItem.getStringField(MenuCategoryFields.NAME)); assertEquals(3, savedItem.getField(MenuCategoryFields.SUCCESSION)); assertEquals(ROLE_VISIBLE, savedItem.getStringField(MenuCategoryFields.AUTH_ROLE)); }
@Test public final void shouldDoNothingIfCategoryAlreadyExists() { MenuCategoryDefinition menuCategoryDefinition = new MenuCategoryDefinition(PLUGIN_IDENTIFIER, "categoryName", ROLE_VISIBLE); Entity categoryEntity = mock(Entity.class); given(menuCrudService.getCategory(menuCategoryDefinition)).willReturn(categoryEntity); menuService.createCategory(menuCategoryDefinition); verify(menuCrudService, never()).save(any(Entity.class)); }