尝试和捕获块覆盖的JUnit [英] JUnit for both try and catch block coverage

查看:90
本文介绍了尝试和捕获块覆盖的JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为下面的代码编写一个JUnit,但是我不知道如何覆盖catch块语句中编写的代码.请任何人都可以为以下代码编写示例JUnit.

I am trying to write a JUnit for below code but I am not getting any idea how to cover the code which is written in catch block statement. Please can any one write a sample JUnit for below code.

在这里,我不想覆盖任何异常,但是想覆盖使用Mockito在catch块中编写的代码行.

Here I don't want to cover any exception, but want to cover the lines of code written in the catch block using Mockito.

public Product getProductLookUpData() {

        Product product = null;
        try{
            // Try to get value from cacheable method
            product = productCacheDao.getProductLookUpData();
            .....//statements
        } catch (Exception ex) {
            // getting value from db
            product = productDao.getIpacMetricCodeLookUpData();
            ....//statements
        }

        return product;
    }

推荐答案

您可以模拟productCacheDaoproductDao并检查在您的测试案例中这些方法被调用了多少次.您可以使用这些模拟对象来模拟异常抛出,如下所示:

You can mock both productCacheDao and productDao and check how many times those methods were invoked in your test cases. And you can simulate exception throwing with those mock objects, like this:

when(mockObject.method(any())).thenThrow(new IllegalStateException());

所以,对于您的情况,我会做这样的事情:

So, for your case I would do something like this:

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.*;

public class ProductTest {
    private static final Product CACHED_PRODUCT = new Product("some parameters for cached product");
    private static final Product DB_PRODUCT = new Product("some parameters for DB product");

    private ProductService service;
    private ProductDao productDaoMock;
    private ProductCacheDao productCacheDaoMock;

    @Before
    public void setup() {
        service = new ProductService();
        productDaoMock = mock(ProdoctDao.class);
        service.setProductDao(productDaoMock);

        productCacheDaoMock = mock(ProdoctCacheDao.class);
        service.setProductCacheDao(productCacheDaoMock);
    }

    @Test
    public void testCache() {
        when(productCacheDaoMock.getProductLookUpData(any())).thenReturn(CACHED_PRODUCT);

        final Product product = service.getProductLookUpData();

        Assert.assertEquals(CACHED_PRODUCT, product);
        verify(productCacheDaoMock, times(1)).getProductLookUpData(any());
        verify(productDaoMock, never()).getIpacMetricCodeLookUpData(any());
    }

    @Test
    public void testDB() {
        when(productCacheDaoMock.getProductLookUpData(any())).thenThrow(new IllegalStateException());
        when(productDaoMock.getIpacMetricCodeLookUpData(any())).thenReturn(DB_PRODUCT);

        final Product product = service.getProductLookUpData();

        Assert.assertEquals(DB_PRODUCT, product);
        verify(productCacheDaoMock, times(1)).getProductLookUpData(any());
        verify(productDaoMock, times(1)).getIpacMetricCodeLookUpData(any());
    }
}

这篇关于尝试和捕获块覆盖的JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆