Issue
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.
Solution
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.
Answered By - Rusty Shackleford
Answer Checked By - Mary Flores (JavaFixing Volunteer)