繁体   English   中英

Junit 5 - 没有为参数注册 ParameterResolver

[英]Junit 5 - No ParameterResolver registered for parameter

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

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

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

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

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

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

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

然后你有两个选择:

  1. 自行创建WebDriver - 例如,在@BeforeAll@BeforeEach方法中。
  2. 使用Selenium Jupiter等扩展程序来帮助您管理WebDriver

我还得到了 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.jupiter.api.Test替换 import import org.junit.Test ,要么

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

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

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

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

}

对我来说,我正在使用

@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) {

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

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

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

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

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

这可能不是上述问题的答案,但对于使用Spring-BootLombok s @RequiredArgsConstructor 的我来说,JUnit 无法自动装配依赖项。 我的课看起来像这样:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { TestPersistenceConfiguration.class, MyRepositoryTest.TestConfiguration.class })
@RequiredArgsConstructor
class MyRepositoryTest
{
    private final MyRepository repository;
    private final TransactionTemplate tt;

// test methods...

    @Configuration
    @EnableJpaRepositories(basePackageClasses = { MyRepository.class })
    static class TestConfiguration {}
}

我只需将onConstructor = @__(@Autowired)添加到 RequiredArgsConstructor 注释中:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { TestPersistenceConfiguration.class, MyRepositoryTest.TestConfiguration.class })
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class MyRepositoryTest
{
// everything as before
}

在测试方法中删除构造函数并添加为参数,解决了我在JUnit 5中的问题。

@ParameterizedTest
@MethodSource("data")
public void check(String input, boolean expected) {
    assertThat(inpu).isEqualTo(expected);
}

private static Stream<Arguments> data() {
    return Stream.of(
            Arguments.of("A", false),
            Arguments.of("B", false)
    );
}

我在使用错误的分隔符(“;”而不是“,”)时遇到了同样的错误

如果您使用 Kotlin 和 Spring Boot,使用主构造函数注入依赖项(如您通常所做的那样)会引入此错误。 解决方案:使用属性注入而不是构造函数注入。

不工作:

@SpringBootTest
class Failing(val someDependency: SomeDependency) {
    @Test
    fun foo() {}
}

在职的:

@SpringBootTest
class Working {
    @Autowired lateinit var someDependency: SomeDependency

    @Test
    fun foo() {}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM