簡體   English   中英

當被告知時,Powermock / mockito不會引發異常

[英]Powermock/mockito does not throw exception when told to

我本以為應該通過以下測試,但是永遠不會拋出異常。 有什么線索嗎?

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

}

這是因為在靜態方法中語法錯誤地使用了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);
    }
}

也可以看看

帶有導入的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);
    }
}

測試通過:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM