有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Junit 5没有为参数注册ParameterResolver

我可以在没有任何特殊测试框架的情况下编写和执行Selenium脚本,但我想使用Junit 5(因为我们与其他工具有依赖关系),而且在使用Junit 4时,我从未见过这样的错误

目前是JUnit5和 我在谷歌上搜索了一下,想了解一些情况,但无法解决这个问题

使用JUnit 5Eclipse 4.8Selenium测试脚本:

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public  class loginTest  {
    public  WebDriver driver = null;

    public loginTest(WebDriver driver) {
        this.driver=driver;
    }

    @BeforeEach
    public void setUp() throws Exception {
        driver.get("google.com");
        System.out.println("Page title is: " + driver.getTitle());
    }

    @Test
    public void test() {
        // some action here I have in original script
        System.out.println("Page title is: " + driver.getTitle());
    }

    @AfterEach
    public void tearDown() throws Exception {
        driver.quit();
    }
}

堆栈跟踪:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.openqa.selenium.WebDriver arg0] in executable [public login.loginTest(org.openqa.selenium.WebDriver)]. at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:191)


共 (6) 个答案

  1. # 1 楼答案

    我遇到了类似的问题,我通过删除@Test注释解决了这个问题 并介绍注释@ParameterizedTest

  2. # 2 楼答案

    我还得到了JUnit 5的ParameterResolutionException

    org.junit.jupiter.api.extension.ParameterResolutionException: 
    No ParameterResolver registered for parameter [int[] arg0] in constructor (public my_package.MyClass(int[]))
    

    我在测试的类中编写了@Test方法

    此错误可以通过两种方式修复:

    1)用import org.junit.Test替换import org.junit.jupiter.api.Test,或

    2)在单独的TestClass中编写测试

  3. # 3 楼答案

    我让@Test@ParameterizedTest都注释了相同的方法。我去掉了前者

  4. # 4 楼答案

    正如Marc Philipp在评论中提到的,您需要确保JUnit Jupiter能够实例化您的测试类

    对于特定的场景,需要删除接受WebDriver的自定义构造函数

    那么你有两个选择:

    1. 自己创建WebDriver——例如,在@BeforeAll@BeforeEach方法中
    2. 使用诸如Selenium Jupiter之类的扩展来帮助您管理WebDriver
  5. # 5 楼答案

    当您试图在同一测试类中同时使用@Test@ParameterizedTest时,会出现此错误。删除@Test注释将解决此问题

  6. # 6 楼答案

    对我来说,我用的是

    @ParameterizedTest
    @CsvSource({
            "1st-param1","1st-param2",
            "2nd-param1","2nd-param2"
            })
    public void isCompleted(String param1, String param2) {
    

    而不是(引用错误):

    @ParameterizedTest
    @CsvSource({
            "1st-param1,1st-param2",
            "2nd-param1,2nd-param2"
            })
    public void isCompleted(String param1, String param2) {