68

我可以在没有任何特殊测试框架的情况下编写和执行 Selenium 脚本,但我想使用 Junit 5(因为我们依赖于其他工具)并且org.junit.jupiter.api.extension.ParameterResolutionException在使用 Junit 4 时我从未见过这样的错误。

目前它是 Junit 5,我用谷歌搜索它以获得某种想法,但无法解决问题。

JUnit 5使用,Eclipse 4.8和测试脚本Selenium

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:没有为可执行文件 [public login.loginTest(org.openqa.selenium.WebDriver)] 中的参数 [org.openqa.selenium.WebDriver arg0] 注册的 ParameterResolver。在 org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:191)

4

11 回答 11

136

我有两者@Test@ParameterizedTest注释相同的方法。我删除了前者。

于 2019-11-18T16:37:19.767 回答
23

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

于 2020-01-15T04:13:46.887 回答
17

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

对于您的特定场景,您需要删除接受WebDriver.

然后你有两个选择:

  1. 自行创建WebDriver- 例如,在@BeforeAllor@BeforeEach方法中。
  2. 使用Selenium Jupiter之类的扩展程序来帮助WebDriver您管理。
于 2018-08-20T10:16:25.953 回答
12

我也得到ParameterResolutionException了 JUnit 5。

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.jupiter.api.Testimport org.junit.Test, 或

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

于 2019-08-29T14:04:54.143 回答
8

我有类似的问题,我通过删除@Test注释并引入注释来解决它@ParameterizedTest

于 2021-08-31T15:02:42.330 回答
3

我收到此错误是因为我的测试需要首先运行我的 Spring Boot 服务器,这样才能执行使用 @Autowired 的依赖注入。我添加了这些注释:

@Transactional
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Server.class)
public MyTestClass () {
...

}
于 2020-03-20T12:25:50.033 回答
3

对我来说,我正在使用

@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) {
于 2021-03-18T23:04:04.850 回答
1

用 @ExtendWith(MockitoExtension.class) 注释测试类对我有用

于 2020-05-27T11:33:23.317 回答
0

在我的情况下,我有 2 个参数。第一个参数使用@AggregateWith,第二个参数不应该被聚合并且已经是正确的类型,但是 JUnit 也尝试聚合它。

切换参数 0 和 1 为我解决了这个问题(也就是说,使用 @AggregateWith 注释的参数现在位于末尾)。

于 2021-05-02T12:14:49.090 回答
0

我在使用 junit4 和 junit5 时遇到了与我的依赖项类似的问题。我@Tests从 junit4 使用,即import org.junit.Test. 这解决了原因。

于 2022-01-12T17:52:12.400 回答
0

我认为您项目中的 WebDriver 类未注释为 bean 并且无法注入,我遇到了同样的问题,当我将注入方式从构造函数注入更改为实例变量注入时,它会抛出 NoSuchBeanDefinitionException ("No qualifying bean of type '...service.RsRepositoryService' available:"),它没有找到我们试图注入的 bean。希望这可以帮助解决这个问题的人:)

于 2022-01-26T15:25:38.587 回答