java Powermock/mockito 在被告知时不会抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14954322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:00:19  来源:igfitidea点击:

Powermock/mockito does not throw exception when told to

javamockitopowermock

提问by Jan-Olav Eide

I would have assumed that the following test should pass, but the exception is never thrown. Any clues ?

我会假设以下测试应该通过,但永远不会抛出异常。任何线索?

@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticService.class)
public class TestStuff {

    @Test(expected = IllegalArgumentException.class)
    public void testMockStatic() throws Exception {
        mockStatic(StaticService.class);
        doThrow(new IllegalArgumentException("Mockerror")).when(StaticService.say("hello"));
        verifyStatic();
        StaticService.say("hello");
}

}

}

回答by Matt Lachman

It's because you are using the doThrow...when syntax incorrectly for static methods. There are a couple different ways to approach this which I've outlined in two separate test methods below.

这是因为您正在使用 doThrow...when 语法错误地用于静态方法。有几种不同的方法可以解决这个问题,我在下面的两个单独的测试方法中概述了这些方法。

@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticService.class})
public class StaticServiceTest {

    @Test (expected = IllegalArgumentException.class)
    public void testMockStatic1() throws Exception {
        String sayString = "hello";
        mockStatic(StaticService.class);
        doThrow(new IllegalArgumentException("Mockerror")).when(
            StaticService.class, "say", Matchers.eq(sayString));
        StaticService.say(sayString);
        fail("Expected exception not thrown.");
    }

    @Test (expected = IllegalArgumentException.class)
    public void testMockStatic2() throws Exception {
        String sayString = "hello";
        mockStatic(StaticService.class);
        doThrow(new IllegalArgumentException("Mockerror")).when(
            StaticService.class);
        StaticService.say(Matchers.eq(sayString)); //Using the Matchers.eq() is
                                                   //optional in this case.

        //Do NOT use verifyStatic() here. The method hasn't actually been
        //invoked yet; you've only established the mock behavior. You shouldn't
        //need to "verify" in this case anyway.

        StaticService.say(sayString);
        fail("Expected exception not thrown.");
    }

}

For reference, here was the StaticService implementation I created. I don't know if it matches yours but I did verify that the tests pass.

作为参考,这里是我创建的 StaticService 实现。我不知道它是否与您的匹配,但我确实验证了测试通过。

public class StaticService {
    public static void say(String arg) {
        System.out.println("Say: " + arg);
    }
}

See Also

也可以看看

回答by javaPlease42

StaticServiceTest.javawith imports:

带有导入的StaticServiceTest.java

import static org.junit.Assert.fail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StaticService.class })
public class StaticServiceTest {

    @Test(expected = IllegalArgumentException.class)
    public void testMockStatic1() throws Exception {
        /* Setup */
        String sayString = "hello";
        PowerMockito.mockStatic(StaticService.class);

        /* Mocks */
        PowerMockito.doThrow(new IllegalArgumentException("Mockerror")).when(
                StaticService.class, "say", Matchers.eq(sayString));

        /* Test */
        StaticService.say(sayString);

        /* Asserts */
        fail("Expected exception not thrown.");
    }

    @Test(expected = IllegalArgumentException.class)
    public void testMockStatic2() throws Exception {
        /* Setup */
        String sayString = "hello";
        PowerMockito.mockStatic(StaticService.class);

        /* Mocks */
        PowerMockito.doThrow(new IllegalArgumentException("Mockerror")).when(
                StaticService.class);
        StaticService.say(Matchers.eq(sayString));

        /* Test */
        StaticService.say(sayString);

        /* Asserts */
        fail("Expected exception not thrown.");
    }

}

StaticService.java

静态服务.java

public class StaticService {
    public static void say(String arg) {
        System.out.println("Say: " + arg);
    }
}

Tests pass fine:

测试通过:

enter image description here

enter image description here