簡體   English   中英

如何測試調用父類受保護(不需要的)方法的方法?

[英]How can I test a method which invoke protected (unwanted) methods of parent class?

我被困在一個非常奇怪的案例中。 我有一些需要測試的特定代碼。 這里是:

public class A {

    /*
     * The real method of real class is so big that I just don't want to test it.
     * That's why I use throwing an exception.
     */
    protected void method(Integer result) {
        throw new RuntimeException("Oops!");
    }

    protected <T> T generifiedMethod(String s, T type) {
        throw new RuntimeException("Oops!");
    }

    protected void mainMethod(Integer value) {
        throw new RuntimeException("Oops!");
    }
}

我也有一個子類:

public class B extends A {

    @Override
    protected void mainMethod(Integer value) {
        if (value == 100500) {
            Integer result = super.generifiedMethod("abc", 100);
            super.method(result);
        }
        super.mainMethod(value);
    }
}

我需要用測試來覆蓋子類。

我嘗試了很多與 PowerMockito 的組合,但沒有一個可以驗證父類受保護方法的調用。 另外,我對僅使用 Mockito、PowerMockito 和 TestNG 有限制。

這是我的測試代碼(變體之一):

@Test
public void should_invoke_parent_logic_methods_of_A_class() throws Exception {

    /* Given */
    A aSpy = PowerMockito.spy(new A());

    PowerMockito.doReturn(250).when(aSpy, "generifiedMethod", "abc", 100);
    PowerMockito.doNothing().when(aSpy, "method", 250);
    PowerMockito.suppress(method(A.class, "mainMethod", Integer.class));

    /* When */
    aSpy.mainMethod(100500);

    /* Then */
    /**
     * Here I need to verify invocation of all methods of class A (generifiedMethod(), method(),
     * and mainMethod()). But I don't need them to be invoked because their logic is unwanted
     * to be tested in case of tests for class B.
     */
}

對於如何測試 B 類的任何建議,我將不勝感激。謝謝。

更新

如果我添加到Then部分此代碼

Mockito.verify(aSpy, times(3)).mainMethod(100500);
Mockito.verify(aSpy, times(1)).generifiedMethod("abc", 100);
Mockito.verify(aSpy, times(1)).method(250);

它給了我以下錯誤消息:

Wanted but not invoked:
a.generifiedMethod("abc", 100);

您是否考慮使用變體來更改類的設計並使用組合而不是繼承? 然后,您將能夠模擬/監視類 A 的實例並將其注入到類 B 的實例中。在這種情況下,您將能夠配置您需要的任何行為。

我真的不確定 doCallRealMethod() 會為你做伎倆,因為你可以選擇模擬方法或調用真正的方法,但不能同時調用。

我認為你不應該使用模擬庫,而是恢復到舊的專用存根模式。

您必須創建具有以下功能的專用A:

  • 請注意,已調用方法methodgenerifiedMethodmainMethod
  • 重置上述指標
  • 提供對上述指標的讀取權限。

然后,您可以使用Junit或TestNG構建測試。 恕我直言的主要問題是你可能必須使用自定義構建過程來加載A存根類而不是真正的A.

如果我理解正確,您想測試 B 類中的方法“mainMethod”。因此,您應該模擬 B 類中的對象,而不是 A 類中的對象。試試這個:

/* Given */
B b = Mockito.mock(B.class);

//use this to skip the method execution
Mockito.doNothing().when(b).generifiedMethod(Mockito.anyString(), Mockito.any());
Mockito.doNothing().when(b).method(Mockito.anyInt());

//or use this to return whatever you want when the methods are called
Mockito.doReturn(new Object()).when(b).method(Mockito.anyInt());
Mockito.doReturn(new Object()).when(b).generifiedMethod(Mockito.anyString(), Mockito.any());

然后你可以從 B 調用 mainMethod,已經知道其他方法將返回什么。

暫無
暫無

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

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