法的にMockitoモックオブジェクトをキャストする方法はありますか?

エール低:

私はJavaの11、JUnitの木星5.6.0、3.3.3 Mockitoに私の小さな確執ボットのためのテストケースクラスのカップルを書いた、とJava不一致API 4.1.1_122。

私は私のテストケースは、同じ目的のために使用される同等のモックの宣言、およびの繰り返しを含む繰り返し動作の複数のインスタンス、持っていたことに気づいたgiven([method call]).willReturn([object]);私の中の@BeforeEach起動方法を。私は自分自身を整理するために、ベースクラスに繰り返し動作を分離することを望んだが、私はこだわっています。私は名前のトップレベルのクラスを作ったので、私のテストクラスのすべては、Javaの不一致APIフレームワークでのイベントハンドラのいくつかの種類をテストしますGenericEventTest

public abstract class GenericEventTest {
    @Mock
    protected GenericEvent eventMock;

    @Mock
    protected JDA jdaMock;

    protected GenericEventTest() {
        MockitoAnnotations.initMocks(this);

        given(   eventMock.getJDA()   )
                .willReturn(jdaMock);
    }
}

私はその後、指定入れ、多形私のテストクラスに合わせてイベントのハイレベルのモックを実装することを望んだgiven()私のテストの全てが同じ行を繰り返しているので、それが属しているクラスのコマンドを。次に、私は私のテストの全てが確執の中のメッセージに関連したイベントのいくつかの種類をテストしたことに気づきました。だから私は、拡張GenericEventと作られましたGuildMessageEventTest

public abstract class GuildMessageEventTest extends GenericEventTest {
    @Mock
    protected GenericGuildMessageEvent eventMock;

    @Mock
    protected TextChannel eventChannelMock;

    @Mock
    protected MessageAction messageActionMock;

    protected GuildMessageEventTest() {
        super();

        MockitoAnnotations.initMocks(this);

        given(   eventMock.getChannel()   )
                .willReturn(eventChannelMock);

        given(   eventChannelMock.sendMessage(anyString())   )
                .willReturn(messageActionMock);
    }
}

私の考えでは、このクラスでのイベントの次のレベル、実装することであるGenericGuildMessageEvent同じ必要がありますgiven()から、それに適用される規則をGenericEventして、適切な追加given()このクラスのルールを。しかし、私は私の問題を発見したと信じています。

私は宣言すると@Mock protected GenericGuildMessageEvent eventMock;私はシャドウイングGenericEvent eventMockからGenericEventTestだから、私は宣言していますGenericGuildMessageEventインスタンスを、私はないです保持given()モック内に添付されたルールをGenericEventTest以下のような何かやっ@Mock protected GenericGuildMessageEvent eventMock = (GenericGuildMessageEvent) super.eventMock;て注釈付きオブジェクトがあるため、意志ではない仕事を@Mock合法的に、このようにキャストすることはできません。

私の質問はこれです:私は合法的に私のインスタンスをキャストすることができますどのようGenericEventGenericGuildMessageEventMockitoは、コピーしたながらgiven()に添付ルールGenericEventスーパークラスでインスタンスを?

VinPro:

宣言abstract GenericEvent getEventMock();あなたのメソッドをGenericEventTestしているGuildMessageEventTest、それを実装します。そして、インスタンスフィールドを移動しprotected GenericEvent eventMock、サブクラスにあなたの基本クラスから。使用して、getEventMock()基本クラスであなたのモックにアクセスします。MockitoAnnotations.initMocks(this);お使いのサブクラスで再び必要とされていません。これは、あなたの基本クラスから嘲笑振る舞いを上書きします。

// GenericEventTest
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.BDDMockito.given;

public abstract class GenericEventTest {

    protected GenericEventTest() {
        MockitoAnnotations.initMocks(this);

        given(getEventMock().getJDA()).willReturn("jdaMock");
    }

    abstract GenericEvent getEventMock();
}

// GuildMessageEventTest
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.BDDMockito.given;

public abstract class GuildMessageEventTest extends GenericEventTest {
    @Mock
    protected GenericGuildMessageEvent eventMock;

    @Mock
    protected GenericGuildMessageEvent myMock;

    @Override
    public GenericGuildMessageEvent getEventMock() {
        return eventMock;
    }

    protected GuildMessageEventTest() {
        super();

//        MockitoAnnotations.initMocks(this);

        given(eventMock.getChannel())
                .willReturn("eventChannelMock");

    }
}

//The Test
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class ActualMessageTest extends GuildMessageEventTest {

    @org.junit.jupiter.api.Test
    public void testX() {
        assertEquals("eventChannelMock", eventMock.getChannel());
        assertEquals("jdaMock", eventMock.getJDA());
        assertNotNull(myMock);
    }

}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=365571&siteId=1