簡體   English   中英

使用 PowerMockito 在 JUnit5 中模擬 Static 方法

[英]Mock Static Methods in JUnit5 using PowerMockito

需要有關使用 JUnit5 和 PowerMockito 框架的 Mocking Static 方法的幫助。

Powermock junit5 和 mockito2.x 不工作 RunnerTestSuiteChunker not found

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

import static org.mockito.Mockito.*;


@PrepareForTest(EnvironmentUtils.class)
@RunWith(PowerMockRunner.class)
public class RuleControllerTest {       

        @Before
        public void setup() {           
            PowerMockito.mockStatic(EnvironmentUtils.class);
        }   


        @Test
        public void test_rule_create_rule() throws IOException {
            when(EnvironmentUtils.isCF()).thenReturn(true);

        }
}

和 pom.xml

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.23.4</version>
    <scope>test</scope>
</dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.2</version>
            <scope>test</scope>
        </dependency>

我從這里跟蹤 Junit5 示例,1) https://www.baeldung.com/junit-5 2) Junit5 模擬 static 方法

但是面臨一個問題,我知道使用 powermock 的 Junit5 存在一個現有問題,但是任何人都知道使用 powermock 使用 JUnit5 來模擬 static 方法的任何其他方法。

使用 mockito v 3.4.0 我們可以簡單地使用沒有 powermock 庫的 mockStatic()方法:

 try (MockedStatic mocked = mockStatic(Foo.class)) {
   mocked.when(Foo::method).thenReturn("bar");
   assertEquals("bar", Foo.method());
   mocked.verify(Foo::method);
 }

assertEquals("foo", Foo.method());

最新文檔和示例: https://javadoc.io/static/org.mockito/mockito-core/3.5.10/org/mockito/Mockito.html#static_mocks

您正在使用 @Before ,這是 junit4 注釋.. Junit5 有 @BeforeEach / @BeforeAll (以適合您的要求為准)。 此外,@Test 的導入來自 junit4,但不是來自 junit5(應該是 org.junit.jupiter.api.Test;)

正如您的鏈接所暗示的那樣,您仍然不能直接使用 junit-5 進行電源模擬,這僅僅是因為 junit-5 仍然沒有可用的PowerMockRunner (擴展)。

但是,在您上面的代碼中,可能出了問題的是這一行。

when(EnvironmentUtils.isCF()).thenReturn(true);

在這里,請注意您使用whenmockito的時間(通過import static org.mockito.Mockito.*;

相反,您必須使用PowerMockito之一。 所以

刪除此行import static org.mockito.Mockito.*;

相反,添加這個。 import static org.powermock.api.mockito.PowerMockito.*;

刪除static 導入,它會正常工作,因為when()調用指向Mockito而不是PowerMock

暫無
暫無

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

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