简体   繁体   English

我怎样才能让 DateUtil 抛出异常?

[英]How can i make DateUtil throw an exception?


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

I am doing unit testing on the above piece of code, and am trying to get 100% line coverage by hitting the Exception clause, however, i cant seem to make it work with my implementation.我正在对上面的代码进行单元测试,并试图通过点击 Exception 子句来获得 100% 的行覆盖率,但是,我似乎无法让它与我的实现一起工作。 Would appreciate any help yall.将不胜感激任何帮助。 Please look below for my implementation.请在下面查看我的实现。

 @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);
    } 

Since DateUtil.clean() is a static method, you can't mock it with Mockito.由于DateUtil.clean()是一个 static 方法,因此不能用 Mockito 模拟它。 You need to use PowerMockito instead:您需要改用 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>

Check official PowerMock documentation for version compatibility with JUnit and Mockito.检查官方 PowerMock 文档以了解与 JUnit 和 Mockito 的版本兼容性。

Once you did that, you should add the following to your test class:一旦你这样做了,你应该在你的测试 class 中添加以下内容:

@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);
    }
    
 
}

you can use mockito-inline artifact from Mockito to test static methods and follow the below approach,您可以使用来自 Mockito 的mockito-inline工件来测试 static 方法并遵循以下方法,

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

for more info,欲了解更多信息,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使不正确的切入点表达式引发异常? - How can I make an incorrect pointcut expression throw an exception? 如何在扫描仪中抛出异常? - How can I throw an exception in a scanner? 如何在 Java 中抛出一般异常? - How can I throw a general exception in Java? 在这种情况下如何使用抛出异常? - How can I use throw exception in this context? 如何从Web服务引发异常? - How can I throw exception from webservice? 如何使Spring Converter引发自定义异常而不是ConversionFailedException? - How can I make a Spring Converter throw a custom exception instead of a ConversionFailedException? 如果对象不具有可比性,如何使方法抛出异常? - How do I make method throw an exception if objects are not comparable? 如果我在实现该接口的方法中抛出异常,我怎么能不在接口方法签名处抛出异常? - How can I not throw exception at interface method signature If I throw exception in method that implements that interface? 如何为非法反射访问警告抛出异常? - How can I throw an exception for an illegal reflective access warning? 我该如何模拟服务以向返回列表的方法抛出异常? - how can I mock a service to throw an exception a method that returns a List?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM