【问题标题】:How can i make DateUtil throw an exception?我怎样才能让 DateUtil 抛出异常?
【发布时间】:2021-05-24 08:41:20
【问题描述】:

    @Override
        public void contextDestroyed(ServletContextEvent sce) {
            try{
                DateUtil.clean();
            }catch(Exception e){
                LOGGER.error("MyServletContextListener contextDestroyed error: ", e);
            }

我正在对上面的代码进行单元测试,并试图通过点击 Exception 子句来获得 100% 的行覆盖率,但是,我似乎无法让它与我的实现一起工作。将不胜感激任何帮助。请在下面查看我的实现。

 @Test (expected= Exception.class)
    public void test_contextDestroyed_Exception() {

        DateUtil wrapper = Mockito.spy(new DateUtil());
        Exception e = mock(Exception.class);

        when(wrapper).thenThrow(e);
       Mockito.doThrow(e)
                .when(myServletContextListener)
                .contextDestroyed(sce);

        myServletContextListener.contextDestroyed(sce);
    } 

【问题讨论】:

  • 首先你正在尝试模拟静态方法。尝试使用电源模拟。你应该写when(DateUtil.clean ()).thenThrow(e)
  • 嗨@ShirishkumarBari,我试过你提到的,但我得到一个''when (T) in Mockito cannot be applied to (void) reason: no instances(s) of type variable(s) ) T 存在,因此 void 符合 T。" 错误
  • 尝试使用 PowerMockito

标签: java mockito junit4


【解决方案1】:

由于DateUtil.clean() 是一个静态方法,你不能用Mockito 模拟它。 您需要改用 PowerMockito:

<properties>
    <powermock.version>1.6.6</powermock.version>
</properties>
<dependencies>
   <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
   </dependency>
</dependencies>

检查 official PowerMock documentation 以了解与 JUnit 和 Mockito 的版本兼容性。

完成此操作后,您应该将以下内容添加到您的测试类中:

@RunWith(PowerMockRunner.class) //<-- this says to JUnit to use power mock runner
@PrepareForTest(DateUtil.class) //<-- this prepares the static class for mock
public class YourTestClass {
    
    @Test(expected = Exception.class)
    public void test_contextDestroyed_Exception() {
        PowerMockito.mockStatic(DateUtil.class);
        when(DateUtil.clean()).thenThrow(new Exception("whatever you want"));
        //prepare your test
        //run your test:
        myServletContextListener.contextDestroyed(sce);
    }
    
 
}

【讨论】:

  • 嗨,我尝试输入 'when(DateUtil.clean()).thenThrow(new Exception("..."));,但我收到以下错误:''when ( Mockito 中的 T) 不能应用于 (void) 原因:不存在类型变量 T 的实例,因此 void 符合 T。" 错误
  • @ben 你在使用 PowerMockito.when() 吗?检查您的静态导入并确保您使用的不是 Mockito 的那个
  • 是的,我什至尝试过 PowerMockito.when(DateUtil.clean()).thenThrow(new Exception("whatever"));但我仍然收到错误无法解析方法'when(void)'
【解决方案2】:

您可以使用来自 Mockito 的 mockito-inline 工件来测试静态方法并遵循以下方法,

try (MockedStatic<DateUtil> utilities = Mockito.mockStatic(DateUtil.class)) {
            utilities.when(DateUtil::clean).thenThrow(Exception.class);
            // assert exception here
        }

更多信息,

https://frontbackend.com/java/how-to-mock-static-methods-with-mockito

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2020-10-24
    • 2017-07-21
    • 2013-08-25
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多