簡體   English   中英

無法使用 TestNG 和 JMockit 注入 @Test 注釋方法

[英]Cannot inject @Test annotated Method with TestNG and JMockit

所以基本上,我想檢查 test() function 是否調用了 static 方法JOptionPane.showInputDialog("Enter a string.") 這是我到目前為止所取得的成就。

使用的庫:

  1. TestNG 7.4.0
  2. JMockit 1.49

當前測試代碼:

package com.example;

import javax.swing.*;
import org.testng.annotations.Test;
import mockit.Mocked;
import mockit.Verifications;

public class Activity1Test {
    @Test
    public void shouldShowInputDialogOnUpdateTextButtonClick(@Mocked JOptionPane jOptionPane) {
        Activity1 activity1 = new Activity1();
        activity1.test();

        new Verifications() {{ JOptionPane.showInputDialog("Enter a string."); }};
    }
}

測試()function:

public void test() {
    JOptionPane.showInputDialog("Enter a string.");
}

不幸的是,它給了我這個錯誤:

-------------------------------------------------------------------------------
Test set: TestSuite
-------------------------------------------------------------------------------
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.781 s <<< FAILURE! - in TestSuite
com.example.Activity1Test.shouldShowInputDialogOnUpdateTextButtonClick  Time elapsed: 0.434 s  <<< FAILURE!
org.testng.TestNGException: 

Cannot inject @Test annotated Method [shouldShowInputDialogOnUpdateTextButtonClick] with [class javax.swing.JOptionPane].
For more information on native dependency injection please refer to https://testng.org/doc/documentation-main.html#native-dependency-injection

這應該可行,但我從未將 JMockit 與 TestNG 一起使用。在正常的 JMockit(使用 JUnit)中,如果你忘記包含運行時 jmockit 需要的 --javaagent 東西,你會得到類似的東西 - 驗證它在地方。 但是,根據您收到的錯誤,看起來 testng 正在嘗試將 test-method-with-argument 作為參數化測試處理(並且在 jmockit 可以查看它之前它正在中止)。 我認為 testng 也適用於 javaagents,在這種情況下,代理的順序可能會顛倒,不確定。

簡單的修復...將“@Mocked JOptionPane joptPane”從此測試方法的括號內移開,並使其成為此測試中的實例變量 class 而不是(離開測試並進入測試類)。 這將意味着你的方法不再有參數(TestNG 不會抱怨)但不利的一面是,現在這個測試類中的每個測試都模擬了 joptPane(也許沒關系)。

暫無
暫無

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

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