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 JUni
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());
}
}
Mockito, or another mocking framework can be used. You will need two test cases - with and without the exception being thrown. You can mock your DAOs to return a known object so you verify your service is exhibiting the correct behaviour.
I've written a full example below.
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));
}
}
You can use Mocking frameworks like Mockito
In your JUnit Test you can do something like
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
// ...
}
}