繁体   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