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

时间:2013-02-19 09:41:58

标签: java mockito powermock

我原以为应该通过以下测试,但是永远不会抛出异常。有线索吗?

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

}

2 个答案:

答案 0 :(得分:5)

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

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

}

作为参考,这是我创建的StaticService实现。我不知道它是否与你的相符,但我确认测试通过了。

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

另见

答案 1 :(得分:0)

带导入的

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

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

测试通过正常:

enter image description here