1

我有一个junit测试。我在 Spring 4.1 中使用 JUnitParamsRunner,但无法解决问题。

我想用该对象运行测试并制作更像这个测试。但在每次运行中我都会遇到同样的错误。当我在谷歌上寻找其他结果时,我看到的方式和我一样,对他们来说,测试工作正常。

java.lang.IllegalStateException: While trying to create object of class class java.lang.String could not find constructor with arguments matching (type-wise) the ones given in parameters.
    at junitparams.internal.InvokeParameterisedMethod.createObjectOfExpectedTypeBasedOnParams(InvokeParameterisedMethod.java:84)
    at junitparams.internal.InvokeParameterisedMethod.castParamsFromObjects(InvokeParameterisedMethod.java:66)
    at junitparams.internal.InvokeParameterisedMethod.<init>(InvokeParameterisedMethod.java:37)
    at junitparams.internal.ParameterisedTestClassRunner.buildMethodInvoker(ParameterisedTestClassRunner.java:125)
    at junitparams.internal.ParameterisedTestClassRunner.parameterisedMethodInvoker(ParameterisedTestClassRunner.java:118)
    at junitparams.JUnitParamsRunner.methodInvoker(JUnitParamsRunner.java:482)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:273)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:446)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:393)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.NoSuchMethodException: java.lang.String.<init>(java.lang.String, java.lang.String, java.lang.String)
    at java.base/java.lang.Class.getConstructor0(Class.java:3350)
    at java.base/java.lang.Class.getConstructor(Class.java:2152)
    at junitparams.internal.InvokeParameterisedMethod.createObjectOfExpectedTypeBasedOnParams(InvokeParameterisedMethod.java:81)
    ... 20 more

测试代码:

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import junitparams.naming.TestCaseName;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import static org.assertj.core.api.Assertions.assertThat;


@RunWith(JUnitParamsRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CheckAccountTests extends UserActionBaseTests {

   private final static String accountQueryParamName = "account";
   private final static String regionNameQueryParamName = "regionName";
   private final static String regionName = "regionTest";
   private final static String nonExistentEmailAccount = "nonexistentemailaccount@cnoga.com";
   private final static String nonExistentRegionName = "nonExistentRegionName";
   @SuppressWarnings("FieldCanBeLocal")
   private final String emailAccount = "dani@cnoga.com";


   @Test
   public void dummyTest() throws Exception {
       for(int i = 0; i< 1000 ; i++) {
           i++;    
       }
   }


   @Override
   public final String get_relativeUrl() { return super.get_relativeUrl() + "/checkAccount.action"; }


   @SuppressWarnings("unused")
   private Object[] checkAccount_DifferentAccounts_ShouldSucceed_ParametersToTest() {
       return new Object[]{
               new Object[]{"Existent email account", emailAccount, regionName}};
   }

   @Test
   @Parameters(method = "checkAccount_DifferentAccounts_ShouldSucceed_ParametersToTest")
   @TestCaseName("{0}.  Account: {1}, Region: {2}")
   public void checkAccount_DifferentAccounts_ShouldSucceed(String account, String regionName) throws Exception {

       var result = (ExceptionResult) sendRequest(getQueryParams(account, regionName), HttpStatus.OK, SysConstant.SUCCESS);

       assertThat(result.getMsg()).isEqualTo("true");
   }

   private List<NameValuePair> getQueryParams(String account, String regionName) {

       return new ArrayList<>() {
           {
               add(new BasicNameValuePair(accountQueryParamName, account));
               add(new BasicNameValuePair(regionNameQueryParamName, regionName));
           }
       };
   }

   @SuppressWarnings("unused")
   private List<NameValuePair> getQueryParams(String account) {

       return getQueryParams(account, regionName);
   }

}
4

1 回答 1

0

您的checkAccount_DifferentAccounts_ShouldSucceed_ParametersToTest()方法为每次测试执行生成三个参数:

"Existent email account", emailAccount, regionName

您的测试方法只接受两个参数:

checkAccount_DifferentAccounts_ShouldSucceed(String account, String regionName)

您需要确保提供程序方法生成的参数数量与测试方法接受的参数数量相匹配。

于 2019-04-02T10:00:56.273 回答