JUnit for both try and catch block coverage

前端 未结 3 1828
陌清茗
陌清茗 2021-02-06 17:37

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

3条回答
  •  太阳男子
    2021-02-06 18:21

    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
          // ...
      }
    
    }
    

提交回复
热议问题