JUnit 5-没有为参数注册的参数解析器

我可以编写和执行 Selenium 脚本,而不需要任何特殊的测试框架,但是我想使用 JUnit 5(因为我们与其他工具有依赖关系) ,而且我在使用 JUnit 4时从未见过这样的错误 org.junit.jupiter.api.extension.ParameterResolutionException

现在是 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();
}
}

堆栈跟踪:

异常: 在可执行文件[ public login.loginTest (org.openqa.selenium. WebDriver)]中没有为参数[ org.openqa.selenium. WebDriver arg0]注册参数。 参数(ExecutableInvoker.java: 191)

136886 次浏览

As Marc Philipp mentioned in his comment, you need to ensure that JUnit Jupiter can instantiate your test class.

For your particular scenario, you'll need to remove your custom constructor that accepts a WebDriver.

Then you have two options:

  1. Create the WebDriver on your own -- for example, in an @BeforeAll or @BeforeEach method.
  2. Use an extension such as Selenium Jupiter to help manage the WebDriver for you.

I also got ParameterResolutionException with JUnit 5.

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

I had written @Test methods inside the class I was testing.

This error could be fixed in two ways:

1) Either replacing import org.junit.jupiter.api.Test with import org.junit.Test, or

2) Writing tests in a separate TestClass.

I had both @Test and @ParameterizedTest annotating the same method. I removed the former.

This error appears when you try to use both @Test and @ParameterizedTest in the same test class. Removing @Test annotation will resolve the issue.

I got this error because my test needed my Spring Boot server to be running first, so that dependency injection using @Autowired would get executed. I added these annotations:

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


}

Annotating test class with @ExtendWith(MockitoExtension.class) worked for me

For me, I was using

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

Instead of (quotes were wrong):

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

In my situation I had 2 parameters. The first parameter was using @AggregateWith, the second parameter should not have been aggregated and was already of the correct type, but JUnit tried to aggregate it as well.

Switching parameter 0 and 1 solved the issue for me (that is, the parameter annotated with @AggregateWith is now at the end).

I had the similar issue I resolved it by removing the @Test annotation and introduce annotation @ParameterizedTest

I had similar issue as my dependencies where using both junit4 & junit5. I made @Tests to use from junit4 i.e, import org.junit.Test. This fixed the cause.

I think the WebDriver class in your project is not Annotated as a bean and it cannot be injected, i had the same problem and when i changed the injection way from constructor injection into instance variable injection, it throws NoSuchBeanDefinitionException ("No qualifying bean of type '...service.RsRepositoryService' available: "), it does not find the bean that we are trying to inject. Hope this helps someone with this problem :)

It is maybe not an answer to the question above, but for me using Spring-Boot, and Lomboks @RequiredArgsConstructor, JUnit couldn't autowire the dependencies. My class looked like that:

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

I just had to add onConstructor = @__(@Autowired) to the RequiredArgsConstructor annotation:

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

Removing constructor and adding as parameter in test method, solved my problem in 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)
);
}

I had the same error while using a wrong separator (";" instead of ",")