2014-12-23 30 views
0

我在擺弄Mockito和Spring MVC。我正在爲我剛剛編寫的代碼編寫單元測試。如何測試這些類型的方法(來自服務層)

這是我CategoryService類:

@Service 
public class CategoryService { 

    @Autowired 
    @Qualifier("categoryDaoImpl") 
    private CategoryDao categoryDao; 

    public void addCategory(Category category) { 
     category.setId(getLastCategoryId() + 1); 
     categoryDao.addCategory(category); 
    } 

    public Category getCategoryById(int id) { 
     return categoryDao.getCategoryById(id); 
    } 

    public List<Category> getCategories() { 
     return categoryDao.getAllCategories(); 
    } 

    public int getCategoriesCount() { 
     return categoryDao.getCategoriesCount(); 
    } 

    public int getLastCategoryId() { 
     if (categoryDao.getAllCategories().size() == 0) { 
      return 0; 
     } 
     return Collections.max(categoryDao.getAllCategories()).getId(); 
    } 

    public CategoryDao getCategoryDao() { 
     return categoryDao; 
    } 

    public void setCategoryDao(CategoryDao categoryDao) { 
     this.categoryDao = categoryDao; 
    } 

我已經測試CategoryDao有近100%的覆蓋率。

現在我想測試CategoryService,但我不知道如何來測試它,我的意思是像方法:addCategory,getCategoryById,getAllCategories,getCategoiesCount等

他們只是聊到DAO模式,但是如果另一個人改變其邏輯呢?如果你告訴我或者如何寫出這種簡短方法的測試,我會很高興。

至於CategoryService而言,我只寫了試驗getLastCategoryId():

@Test 
    public void shouldGetLastCategoryIdWhenListIsEmpty() { 
     //given 
     List<Category> list = new ArrayList<Category>(); 
     Mockito.when(categoryDao.getAllCategories()).thenReturn(list); 

     //when 
     int lastCategoryId = categoryService.getLastCategoryId(); 

     //then 
     assertThat(lastCategoryId, is(0)); 
    } 

    @Test 
    public void shouldGetLastCategoryIdWhenListIsNotEmpty() { 
     //given 
     List<Category> list = new ArrayList<Category>(); 
     list.add(new Category(1, "a", "a")); 
     list.add(new Category(3, "a", "a")); 
     list.add(new Category(6, "a", "a")); 

     Mockito.when(categoryDao.getAllCategories()).thenReturn(list); 

     //when 
     int lastCategoryId = categoryService.getLastCategoryId(); 

     //then 
     assertThat(lastCategoryId, is(6)); 
    } 

非常感謝您的幫助:)

最好的問候, 湯姆

+0

這是爲什麼使用構造函數注入而不是字段注入的一個很好的例子:您可以使用Mockito或Spock中的模擬DAO創建服務對象並驗證適當的交互。 (我也建議看看Spring Data而不是手寫你自己的DAO類。) – chrylis

回答

1

您需要驗證服務方法是否按照合同行事,即使它們將來被修改。

例如, addCategory(類別c)方法添加類別。這可以通過驗證categoryDao.addCategory()方法與具有所需屬性集的類別對象一起調用來驗證。在這種情況下,應該將id設置爲lastCategoryId。驗證可以簡單地通過創建CategoryDao類的間諜來完成(使用mockito等第三方庫會更簡單。)

getCategoryById(),getCategories()和getCategoriesCount()方法的測試用例可以驗證返回的值我知道這意味着每個方法只有一個測試用例,但這些測試用例只是確認如果在服務方法實現中添加了更多的邏輯,合同保持完好。

下面是一個測試案例addCategory()

public class CategoryServiceTest { 
    private CategoryService service; 
    private CategoryDaoSpy daoSpy; 

    @Before 
    public void setUp() { 
     service = new CategoryService(); 
     daoSpy = new CategoryDaoSpy(); 
     service.setCategoryDao(daoSpy); 
    } 


    @Test 
    public void shouldSaveCategoryWhenCategoryPassed() { 
     Category category = new Category(); 
     service.addCategory(category); 

     assertEquals(daoSpy.getAddCategoryCallCount(), 1); 
     assertEquals(daoSpy.getCategories().size(), 1); 
     assertEquals(daoSpy.getCategories().get(0).getId(), 1); 
    } 

} 

public class CategoryDaoSpy extends CategoryDao { 

    private int addCategoryCallCount = 0; 
    private List<Category> categories = new ArrayList<>(); 

    @Override 
    public void addCategory(Category category) { 
     this.addCategoryCallCount++; 
     categories.add(category); 
    } 

    public int getAddCategoryCallCount() { 
     return addCategoryCallCount; 
    } 

    public List<Category> getCategories() { 
     return categories; 
    } 

    @Override 
    public List<Category> getAllCategories() { 
     return Collections.emptyList(); 
    } 
} 
+0

好的,我理解你的方法。您能否提供一些例如addCategory或getCategories? 謝謝:) – tomdavies

+0

爲addCategory添加了一個測試用例() –

+0

非常感謝:) – tomdavies