第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JUnit 更改集成測試中的類或方法行為

JUnit 更改集成測試中的類或方法行為

慕沐林林 2023-11-01 16:59:13
我有一個java應用程序(里面沒有Spring),我想用集成測試來測試它。我的主要用例是主要功能,它使用指定的輸入在數(shù)據(jù)庫上執(zhí)行一些操作并向兩個不同的服務(一個 SOAP 和一個 REST)發(fā)送一些請求?,F(xiàn)在我有一個有效的 JUnit 配置(分為單元測試和集成測試)+ io.fabric8:docker-maven-plugin,它在集成測試期間使用 docker 映像作為數(shù)據(jù)庫。我想做的是為這兩個服務添加模擬,特別是用于直接調(diào)用外部服務的方法。最大的問題是我有這樣的結(jié)構(gòu):class A{    Result mainFunction(Request r){        ....        B b = new B(params);        b.logEvent(someParameters)        ....    }}class B{    int logEvent(Object someParameters){        ....        NotifierHandler nh = new NotifierHandler(param1);        nh.sendNotification(json);        ....    }}我在哪里:class NotifierHandler{    String sendNotification(Json j){        ...        [call to REST service with some parameters]        ...        ...        [call to SOAP service with some parameters]        ...    }}我需要什么:A.mainFunction(r)在測試環(huán)境中調(diào)用,用 FakeNotifierHandler 替換 NotifierHandler 和/或更改方法的行為sendNotification()。實際問題:現(xiàn)在使用 Mockito 和 PowerMock 時遇到的問題是,我無法使用 FakeNotifierHandler 全局直接更改類 NotifierHandler。同樣嘗試改變該方法的行為。特別是,我需要的是創(chuàng)建一個class FakeNotifierHandler{    String sendNotification(Json j){        ...        [save on an HashMap what I should send to the REST service]        ...        ...        [save on another HashMap what I should send to the SOAP service]        ...    }}閱讀我嘗試過的所有示例,我只看到了更改方法返回值的簡單示例,而不是更改一個類的一個方法的行為,該方法被另一個類和另一個我用作集成測試起點的類所使用。注意:可能有一種快速的方法可以做到這一點,但我對這種類型的測試(Mockito,PowerMock,...)非常陌生,并且我沒有找到這種特殊奇怪情況的示例。
查看完整描述

1 回答

?
繁星coding

TA貢獻1797條經(jīng)驗 獲得超4個贊

我找到了一個非常有效的解決方案,而且非常簡單!

解決方案是 PowerMock(https://github.com/powermock/powermock),特別是用另一個類實例替換類實例的創(chuàng)建: https: //github.com/powermock/powermock/wiki/mockito#how-模擬新對象的構(gòu)造

我的項目中只有一個問題,那就是 JUnit 5。PowerMock 支持 JUnit 4,因此,僅在解決方案的某些測試中使用它。為了做到這一點,需要更換

import org.junit.jupiter.api.Test;

import org.junit.Test;

為了使用“ whenNew() ”方法,我擴展了測試中必須替換的類,并且僅覆蓋了集成測試所需的方法。此解決方案的最大好處是我的代碼未受影響,并且我也可以在舊代碼上使用此方法,而無需在代碼重構(gòu)期間引入回歸的風險。

關(guān)于集成測試的代碼,這里有一個例子:

import org.junit.jupiter.api.DisplayName;

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;


@RunWith(PowerMockRunner.class)

@PowerMockIgnore({"javax.crypto.*" }) // https://github.com/powermock/powermock/issues/294

@PrepareForTest(LegacyCoreNetworkClassPlg.class) // it is the class that contains the "new SOAPCallHelper(..)" code that I want to intercept and replace with a stub

public class ITestExample extends InitTestSuite {

    @Test

    @DisplayName("Test the update of a document status")

    public void iTestStubLegacyNetworkCall() throws Exception {


        // I'm using JUnit 4

        // I need to call @BeforeAll defined in InitTestSuite.init();

        // that works only with JUnit 5

        init();


        LOG.debug("IN stubbing...");

        SOAPCallHelperStub stub = new SOAPCallHelperStub("empty");

        PowerMockito.whenNew(SOAPCallHelper.class).withAnyArguments().thenReturn(stub);

        LOG.debug("OUT stubbing!!!");


        LOG.debug("IN iTestStubLegacyNetworkCall");

        ...

        // Here I can create any instance of every class, but when an instance of 

        // LegacyCoreNetworkClassPlg.class is created directly or indirectly, PowerMock

        // is checking it and when LegacyCoreNetworkClassPlg.class will create a new

        // instance of SOAPCallHelper it will change it with the 

        // SOAPCallHelperStub instance.

        ...

        LOG.debug("OUT iTestStubLegacyNetworkCall");

    }

}

這里是pom.xml的配置


    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <junit.jupiter.version>5.5.2</junit.jupiter.version>

    <junit.vintage.version>5.5.2</junit.vintage.version>

    <junit.platform.version>1.3.2</junit.platform.version>

    <junit.platform.engine.version>1.5.2</junit.platform.engine.version>

    <powermock.version>2.0.2</powermock.version>


    <!-- FOR TEST -->

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-api</artifactId>

        <version>${junit.jupiter.version}</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>junit</groupId>

        <artifactId>junit</artifactId>

        <scope>test</scope>

    </dependency>

    <!-- Only required to run tests in an IDE that bundles an older version -->

    <dependency>

        <groupId>org.junit.platform</groupId>

        <artifactId>junit-platform-launcher</artifactId>

        <version>${junit.platform.version}</version>

        <scope>test</scope>

    </dependency>

    <!-- Only required to run tests in an IDE that bundles an older version -->

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-engine</artifactId>

        <version>${junit.jupiter.version}</version>

        <scope>test</scope>

    </dependency>

    <!-- Only required to run tests in an IDE that bundles an older version -->

    <dependency>

        <groupId>org.junit.vintage</groupId>

        <artifactId>junit-vintage-engine</artifactId>

        <version>${junit.vintage.version}</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.junit.platform</groupId>

        <artifactId>junit-platform-engine</artifactId>

        <version>${junit.platform.engine.version}</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.junit.jupiter</groupId>

        <artifactId>junit-jupiter-params</artifactId>

        <version>${junit.vintage.version}</version>

        <scope>test</scope>

    </dependency>


    <dependency>

        <groupId>org.powermock</groupId>

        <artifactId>powermock-module-junit4</artifactId>

        <version>${powermock.version}</version>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.powermock</groupId>

        <artifactId>powermock-api-mockito2</artifactId>

        <version>${powermock.version}</version>

        <scope>test</scope>

    </dependency>


查看完整回答
反對 回復 2023-11-01
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號