我正在尝试为下面的代码编写一个JUnit,但我不知道如何覆盖用catch块语句编写的代码。请任何人都可以为下面的代码编写一个示例JUnit。
这里我不想涵盖任何异常,但想要使用Mockito覆盖catch块中编写的代码行。
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;
}
答案 0 :(得分:6)
您可以mock同时productCacheDao
和productDao
并检查在您的测试用例中调用这些方法的次数。您可以使用这些模拟对象模拟异常抛出,如下所示:
when(mockObject.method(any())).thenThrow(new IllegalStateException());
所以,对于你的情况,我会做这样的事情:
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());
}
}
答案 1 :(得分:1)
您可以使用像Mockito
这样的模拟框架在您的JUnit测试中,您可以执行类似
的操作public class YourTest {
private ProductcacheDao productCacheDaoMock;
@Before
public void setup() throws Exception {
productCacheDaoMock = Mockito.mock(ProductcacheDao.class);
}
@org.junit.Test
public void test() {
// Given
Mockito.when(productCacheDaoMock.getProductLookUpData()).thenThrow(new RuntimeException());
// When
// do your stuff
productCacheDaoMock.getProductLookUpData();
// Then
// ...
}
}
答案 2 :(得分:1)
Mockito或其他模拟框架。您将需要两个测试用例 - 抛出异常和不抛出异常。您可以模拟您的DAO以返回已知对象,以便验证您的服务是否表现出正确的行为。
我在下面写了一个完整的例子。
public class MocksExample {
private ProductService service;
@Mock
private ProductDao productDao;
@Mock
private ProductCacheDao productCacheDao;
@Mock
private Product product;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
service = new ProductService();
service.setProductCacheDao(productCacheDao);
service.setProductDao(productDao);
}
@Test
public void serviceReturnsProduct() {
when(productCacheDao.getProductLookUpData()).thenReturn(product);
assertThat(service.getProductLookUpData(), is(product));
}
@Test
public void ipacProductReturnedWhenDaoThrowsException() {
when(productDao.getIpacMetricCodeLookUpData()).thenReturn(product);
when(productCacheDao.getProductLookUpData()).thenThrow(new IllegalStateException("foo"));
assertThat(service.getProductLookUpData(), is(product));
}
}