【问题标题】:how to write Junit test case which covers catch block during code coverage?如何编写覆盖代码覆盖期间的 catch 块的 Junit 测试用例?
【发布时间】:2014-06-17 19:22:27
【问题描述】:

以下是代码,我想知道如何编写 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 块的测试用例。

【问题讨论】:

标签: java junit mockito


【解决方案1】:

将 System.exit(2) 代码放在它自己的类中并存根或模拟它,而不是直接调用该代码。这样,在您的测试中它不会真正退出,但您会知道在生产环境中它会退出。

如果您不知道如何处理此问题(模拟或存根),您应该了解有关 dependency injection 的更多信息。

现在你已经完成了一半。另一半是使用依赖注入来模拟出clothBrandQuery。使其getClothBrandMethod 无论如何都会引发异常。然后你会沿着这条路走下去。一个很好的模拟框架是Mockito。

【讨论】:

  • 是的,我做到了。感谢您的帮助。
【解决方案2】:

您不应该在该方法中使用 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);
    }

}

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多