簡體   English   中英

如何在代碼覆蓋期間編寫涵蓋catch塊的Junit測試用例?

[英]how to write Junit test case which covers catch block during code coverage?

下面是代碼,我想知道如何編寫JUnit測試,以便我的覆蓋范圍可以覆蓋catch塊

import com.pkg.ClothBrandQuery;

Public class MapBrand implements SomeInterface{
   public Brand mapThisBrands(final ClothBrand clothBrand) {

     Brand brand = new Brand();
     try{
        defaultBrand = clothBrandQuery.getClothBrandMethod(ProjectConstants.DEFAULT_BRAND_KEY);
     }
     catch(Exception e){
        logger.fatal("Database error occurred retrieving default cloth brands", e);
        System.exit(2);
     }                              
             if(defaultBrand != null && defaultBrand != clothBrand){
        brand.setBrandName(clothBrand.getBrandName());
        brand.setCompanyName(clothBrand.getCompanyName());
        brand.setBackgroundColor(clothBrand.getBackgroundColor());
             }
             else{
                brand.setBrandName(defaultBrand.getBrandName());
        brand.setCompanyName(defaultBrand.getCompanyName());
        brand.setBackgroundColor(defaultBrand.getBackgroundColor());
             }
    }
}

我能夠為mapThisBrands編寫測試方法以測試品​​牌對象,但我不知道如何測試defaultBrand對象以及我如何編寫一個測試用例,其中catch塊將被執行。

System.exit(2)代碼放在它自己的類和存根中或模擬它而不是直接調用該代碼。 這樣, 在您的測試中它不會真正退出,但您會知道在生產環境中它會。

如果您不知道如何處理(模擬或存根),您應該了解有關依賴注入的更多信息。

現在你已經完成了一半。 另一半是使用依賴注入來模擬clothBrandQuery 使它成為getClothBrandMethod拋出異常。 然后你將走這條路。 一個好的模擬框架是Mockito

你不應該在該方法中使用System.exit(),只在main方法中使用它,然后在mapThisBrands中拋出異常:

public class MapBrand implements SomeInterface{

    public static void main(String[] args) {
        MapBrand map = new MapBrand();
        ClothBrand clothBrand = this.getClothBrand();
        try
        {
            map.mapThisBrands(clothBrand);
        }
        catch (Exception e)
        {
            System.exit(2);
        }
    }

    public Brand mapThisBrands(final ClothBrand clothBrand) {

        Brand brand = new Brand();
        try{
            defaultBrand = clothBrandQuery.getClothBrandMethod(ProjectConstants.DEFAULT_BRAND_KEY);
        }
        catch(Exception e){
            logger.fatal("Database error occurred retrieving default cloth brands", e);
        }
        if(defaultBrand != null && defaultBrand != clothBrand){
            brand.setBrandName(clothBrand.getBrandName());
            brand.setCompanyName(clothBrand.getCompanyName());
            brand.setBackgroundColor(clothBrand.getBackgroundColor());
        }
        else{
            brand.setBrandName(defaultBrand.getBrandName());
            brand.setCompanyName(defaultBrand.getCompanyName());
            brand.setBackgroundColor(defaultBrand.getBackgroundColor());
        }
    }
}

而你的考驗

public class MapBrandTestCase {

    @Test (expected = java.lang.Exception.class)
    public void databaseFailureShouldRaiseException()
    {
        Query clothBrandQuery = Mockito.mock(Query.class);
        when(clothBrandQuery.getClothBrandMethod(any())).thenThrow(new Exception());
        MapBrand mapBrand = new MapBrand(...);
        mapBrand.mapThisBrands(aBrand);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM