5

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

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

}

4

2 回答 2

5

这是因为您对静态方法使用了 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.");
    }

}

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

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

也可以看看

于 2013-02-19T14:03:22.427 回答
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.");
    }

}

静态服务.java

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

测试通过:

在此处输入图像描述

于 2013-11-11T14:48:53.360 回答