首页 > 解决方案 > Junit 5 中的多项测试

问题描述

我研究了如何使用 JUnit 5 并遇到了一个问题。我看了很多示例并阅读了有关此问题的信息,但无法决定。所以,如果我想测试一个测试,这是可行的,我会得到结果。但是,如果我添加多个测试(2 个或更多),则会出错。我的主类代码。

public class Project1_Temp {
Project1_Temp(){
    
}
public static int plusTest(int a, int b) {
    return a+b;
}

}

我的类测试代码

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
class Project1_TempTest {
    int a,b,expResult;
    //@Parameter(0)
    /*public int a;
    
    //@Parameter(1)
    public int b;
    
    //@Parameter(2)
    public int expResult;
    */
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        //Object[][] data = new Object[][] { { 1 , 2, -5 }, { 5, 3, 15 }, { 121, 4, 484 } };
        return Arrays.asList(new Object[][] {{1,2,3},{0,0,0},{5,2,7},{2,2,5}});
    }
    
    Project1_TempTest(int a, int b, int expResult){
        this.a = a;
        this.b = b;
        this.expResult = expResult;
    }
    
    @Test
    void test() {
        assertEquals(expResult, Project1_Temp.plusTest(a, b));
    }

}

我得到这个错误:

no parameterresolver registered for parameter int arg0 in constructor

我查看了不同的论坛,讨论了这个错误,但不明白,我怎么能决定它。请帮帮我 :)

标签: javajunit5

解决方案


推荐阅读