简体   繁体   中英

Junit Parameterized Constructor - Wrong Number of Arguments

I am trying to build a parameterized Junit Test in order to test each occurrence of a LinkedHashMap within an ArrayList of LinkedHashMaps. This is because each LinkedHashMap represents a test case.

The map is built/stored by a object within my TestSuite class, which utilizes the @BeforeClass annotation, as per below:

@RunWith(Suite.class)
public class TestSuite {

public static MapBuilder mapBuilder = new MapBuilder ();

@ClassRule
public static ExternalResource testResource = new ExternalResource();
    @Override
    protected void before () throws IOException {
    mapBuilder.buildMap();
    }
}

The .buildMap() method of Mapbuilder is expensive, so I do not wish to build it for every Junit Test, hence the need for parameterized class, as per below:

@RunWith(Parameterized.class)
public class TestCasesA {

private LinkedHashMap<Integer, Integer> currentMap;

public TestCasesA (LinkedHashMap<Integer, Integer> currentMap) {
    this.currentMap = currentMap;
    }

@Parameters
public static Collection<Object[]> dataFeed () {
    Object[] objArray = TestSuite.MapBuilder.returnBuiltMap().toArray();
    return Arrays.asList(new Object[][] {objArray});
    }

@Test
// Some test which uses currentMap 
}

However, when i run this i keep facing the below issue:

java.lang.IllegalArgumentException: wrong number of arguments at java.lang.reflect.Constructor.newInstance(Unknown Source)

I've been scouring Stack Overflow and other sites about this all day, but i still cannot work it out.

Not sure if this matters or not but my LinkedHashMap may contain between 1 upto around 32 key/value entries (Could contain more, but very unlikely).

I'v followed the plentiful examples online where you manually type what you want to return, and this works fine, but manually typing in is not suitable for what i'm trying to achieve.

I've even done a enhanced for loop within TestCasesB, called the.returnBuiltMap() method and printed out each iteration of the map just to prove my Test Cases can "see" the built map.

Can anyone help?

For reference, I am using Java 7 and Junit 4.

I've been researching this further myself due to lack of any answers (Uploading the wrong code in my original post was a bad move on my part admittedly).

I finally found the answer today - @BeforeClass runs after @Parameters, as discussed in great detail here > https://github.com/junit-team/junit4/issues/527

My above code was always going fail as my data set-up was performed after @Parameters ran, so @Parameters had nothing to feed into the designated constructor.

It seems you have forgotten to add the constructor, something like:

@RunWith(Parameterized.class)
public class TestCasesA {
   private Integer paramA, paramB;

   @Parameters
   public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] { 
         { 1, 1 },
         { 2, 2 }
      });
   }

   public TestCasesA (Integer paramA, Integer paramB){
      this.paramA = paramA;
      this.paramB = paramB;
   }

   @Test
   public void testYourParams() {
      assertEquals(paramA, paramB);
   }
}

I was also fasing this error: java.lang.IllegalArgumentException: wrong number of arguments

after I removed paramether from constructor, but then I found that I also must delete it from @Parameterized.Parameters in companior object. See the code below:

import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@ExperimentalCoroutinesApi
@RunWith(Parameterized::class)
class MyAppClass1UseCaseTest(
    private val expectedFirstParam: String,
    expectedSecondParam: Boolean //If you remove paramether from here, then you also have to remove param from copmanion // object below
) {

    @Test
    fun useCaseTest1() = runBlockingTest {
    }


    companion object {
        @JvmStatic
        @Parameterized.Parameters(name = "firstParam {0}, secondParam {1}") //you also need to remove param from here
        fun data(): Collection<Array<Any?>> {
            return listOf(
                arrayOf("stringValue1", true), //and remove param from here
                arrayOf("stringValue1", false)
            )
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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