JUnit 5 Collection type parameters cause errors with “@ParameterizedTest”

Multi tool use
Multi tool use


JUnit 5 Collection type parameters cause errors with “@ParameterizedTest”



I cannot find the reason of the errors from previously asked questions, because they were about "@Test" (which does not allow custom data types).



I have a program which takes a String type input (which is usually a block of text), and returns input's sentences in the form of List. To test this program properly I tried to store my inputs and expected outputs (which will be compared to the outputs of my program, purpose of the test) in the form of List. Here DataStructure class consists of 2 attributes: a String type attribute, and a List type attribute.



In total, I have some necessary classes about the program itself, SentenceSplitterTest class, DataStructure class, and Collection class. In Collection class I handle the loading process of the List mentioned above. I basically do some file operations and store my inputs and expected outputs inside Collection class. Below here is the content of my ProgramTest.java file.


package Classes;

import org.junit.Before;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@RunWith(Parameterized.class)
public class SentenceSplitterTest {

private String input;
private List<String> output = new ArrayList<>();
private SentenceSplitter sentencesplitter;
private static CollectionClass collectionClass;

static {
try {
collectionClass = new CollectionClass();
} catch (IOException e) {
e.printStackTrace();
}
}

public SentenceSplitterTest(String input, List<String> output) {
super();
this.input = input;
this.output = output;
}

@Before
public void initialize() throws IOException {

sentencesplitter = new SentenceSplitter();
}

@ParameterizedTest
@MethodSource("data")
public void testSentenceSplitterTest() {

System.out.println("Expected output: " + output);
assertEquals(output, sentencesplitter.sentenceSplit(input));
}

public static Collection data() {
return collectionClass.getContent();
}
}



As you can see, I used @MethodSource("data") to set my parameters. My test screen looks like this



I get the following error for InitializationError in the screenshot:



java.lang.Exception: No public static parameters method on class
Classes.SentenceSplitterTest



I get the following error for each element in the screenshot:



org.junit.jupiter.api.extension.ParameterResolutionException: No
ParameterResolver registered for parameter [java.lang.String arg0] in
executable [public
Classes.SentenceSplitterTest(java.lang.String,java.util.List)].
I cannot find what I should do to solve the issues. Please help.



EDIT 1: I am asked to share CollectionClass class.


package Classes;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static java.nio.file.Files.readAllLines;

public class CollectionClass {

private static List<DataStructure> collection = new ArrayList<>();

public List<DataStructure> getContent() {
return collection;
}

public CollectionClass() throws IOException {

List<String> inputs = readAllLines(Paths.get("inputs.txt"), StandardCharsets.UTF_8);
List<String> outputs = readAllLines(Paths.get("outputs.txt"), StandardCharsets.UTF_8);

int i = 0;
int j = 0;
int ctr = 0;

while(i< inputs.size() || j< outputs.size()) {

DataStructure structure = new DataStructure();
String inputIncoming = null;
List<String> outputIncoming = new ArrayList<String>();

if(i< inputs.size()) {

if(!inputs.get(i).equals("-")) {
// nothing

}
else {
i++;
inputIncoming = inputs.get(i);
structure.string = inputIncoming;
}
}

if(j< outputs.size()) {


if(!outputs.get(j).equals("-") && !outputs.get(j).equals("--")) {
// nothing
}
else {
if(outputs.get(j).equals("-")) {
j++;
outputIncoming.add(outputs.get(j));
structure.listOfString = outputIncoming;
}
else if(outputs.get(j).equals("--")) {
j++;
outputIncoming.add(outputs.get(j));
j++;
outputIncoming.add(outputs.get(j));
structure.listOfString = outputIncoming;
}
}
}

collection.add(ctr,structure);

i++;
j++;
ctr++;
}
for(DataStructure structure: collection) {
System.out.println(structure.string);
System.out.println(structure.listOfString);
}
}
}



I want to add DataStructure class, too.


package Classes;

import java.util.ArrayList;
import java.util.List;

public class DataStructure {

public String string;
public List<String> listOfString = new ArrayList<>();
}





What exactly is CollectionClass? Can you share its implementation?
– Mureinik
1 hour ago


CollectionClass





@Mureinik edited the question.
– KBO
1 hour ago




1 Answer
1



You're mixing JUnit 4 and JUnit Jupiter parameterized tests. In JUnit JUpiter, there's no test runner, the constructor remains parameterless and you pass the parameters to the test method (in a way that's a bit reminiscent of JUnit 4's Theories):


public class SentenceSplitterTest {
private SentenceSplitter sentencesplitter;
private static CollectionClass collectionClass;

// Initialization of CollectionClass moved here (instead of a static block) for two
// reasons:
// 1. If the initialization fails, you can't run the test anyway - better fail
// right here that print an error and continue to the test which we
// know won't work
// 2. It just looks neater
@BeforeAll
public static void initializeCollectionClass() throws IOException {
collectionClass = new CollectionClass();
}

@Before
public void initializeSentenceSplitter() throws IOException {
sentencesplitter = new SentenceSplitter();
}

@ParameterizedTest
@MethodSource("data")
public void testSentenceSplitterTest(DataStructure ds) {
String input = ds.string;
List<String> output = ds.listOfString;
assertEquals(output, sentencesplitter.sentenceSplit(input));
}

public static Stream<DataStructure> data() {
return collectionClass.getContent().stream();
}
}





First of all, thank you for your time. This time I get again 2 errors. For InitializationError I get this: java.lang.Exception: No public static parameters method on class Classes.SentenceSplitterTest and for each element I get this: java.lang.NullPointerException
– KBO
1 hour ago






By the way "@RunWith(Parameterized.class)" is still above declaration line of public class SentenceSplitterTest { ..}. Should I remove it?
– KBO
1 hour ago





Exception about nullpointer happens at the line "List<String> output = ds.listOfString;", maybe this can give an idea.
– KBO
53 mins ago





@KBO, yes, you should remove the runner annotation. As you can see, it's not in the answer.
– M. Prokhorov
49 mins ago





@M.Prokhorov Sorry. InitializationError is gone after removing the runner annotation. Others remain.
– KBO
46 mins ago







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Wy9 XmuRYGJVDIACbV0zVETkCCgPpXbA,BTgcFD5Ru8N2LyS,nOfFoFVXXviqQlmfSD
6lk,tDCz3LHCXm9ZROIdVQQtTfVOFYq7LqaacSLpX

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications