1

I have a junit test. I'm using JUnitParamsRunner in spring 4.1 and I can't resolve the problem.

I want to run the test with the object and make more like this test. but in every run I getting the same error. when I looked for other result in google, I saw same way like I did and for them the tests work fine.

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

the test code:

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);
   }

}
nadav3200
  • 11
  • 1
  • 5

1 Answers1

0

Your checkAccount_DifferentAccounts_ShouldSucceed_ParametersToTest() method produces three parameters for each test execution:

"Existent email account", emailAccount, regionName

Your test method accepts only two parameters:

checkAccount_DifferentAccounts_ShouldSucceed(String account, String regionName)

You need to make sure that the number of parameters produced by the provider method matches the number of parameters that the test method accepts.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102