【问题标题】:No tests found for parameterized JUnit4 test未找到参数化 JUnit4 测试的测试
【发布时间】:2016-02-04 20:35:36
【问题描述】:

我正在尝试根据之前在我的一个 src/main 类中设置的 queue 参数化 JUnit4 测试。这是我到目前为止所做的,有一个测试套件的类(MigratorTestSuite

@RunWith(Suite.class)
@Suite.SuiteClasses({ParameterizedTest.class})
public class MigratorTestSuite {
    @BeforeClass
    public static void setUp() throws SAXException, ParserConfigurationException, GitAPIException, IOException {
        Migrator.getReady();
    }

@AfterClass
public static void tearDown() throws SQLException {
    DatabaseManager.closeConnections();
    RepositoryManager.closeRepository();
}

}

还有一个 ParameterizedTest 类,我正在研究如何运行 parameterized JUnit test,如下所示:

@RunWith(Parameterized.class)
public class ParameterizedTest {

    @Parameterized.Parameters(name="whatever")
    public static Queue<Deque<String>> data(){
        return TestCasesConstructor.testCasesQueue;
    }

    private Deque<String> scenario;

    public ParameterizedTest(Queue<Deque<String>> q){
        scenario = q.peek();
    }

    @Before
    public void initialize() throws ParserConfigurationException, IOException, SQLException, ClassNotFoundException {

        System.out.println("--- Preparing database for running scripts");
        DatabaseManager.dropDatabase();
        DatabaseManager.createDatabase();
    }

    @Test
    public void testPlainMigration() throws Exception {
        Assert.assertTrue(Migrator.runScenario(this.scenario));
    }

    @After
    public void after() throws SQLException {
        DatabaseManager.closeConnections();
        TestCasesConstructor.testCasesQueue.remove();
    }
}

当我执行mvn clean install test -Dtest=MigratorTestSuite 时,结果是它没有找到任何测试,当我调试它时,它显示:

没有找到与org.junit.runner.Request 中的任何参数匹配的数据的测试 在org.junit.internal.requests.FilterRequest.getRunner

我做错了什么?我应该更好地在 TestNG 中实现它吗?我对 Junit 真的很陌生。

【问题讨论】:

    标签: java maven intellij-idea junit junit4


    【解决方案1】:

    你可以试试这个:

    mvn clean install -Dtest=MigratorTestSuite test 
    

    【讨论】:

      【解决方案2】:

      data 方法应返回对象数组的集合,即Collection&lt;Object[]&gt;。每个对象数组都是一个测试向量,包含例如输入字符串和预期结果。

      测试类构造函数应该将测试向量的元素作为参数。在上面的示例中,它将接受一个字符串和一个结果参数,您通常会将其存储在一个字段中,以便实际的测试用例可以使用它。对于每个测试向量,都会创建一个新的测试类实例。

      在您的情况下,您似乎想使用一系列场景作为参数。

      因此,您的数据方法应该返回一个场景集合-数组(每个数组的长度为 1),并且您的构造函数应该使用一个 single 场景(而不是完整的集合;因此无需“偷看”)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-03
        • 2011-05-22
        • 2022-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-24
        相关资源
        最近更新 更多