javajunitjunit-runner

Junit test where some parameters differ and other remains same


I have a following input parameters for a junit Test.

Basically I need to test an algorithm which takes inputFile and few other parameters as input and produces some data. This data need to be compared with the referenceData (referenceData file is also one of the input parameters to the test). If the data produced by algorithm are same as reference data, then the test passes else it fails.

inputFile // .xml File - is different for each test. there are total five.
param 1   //remains same
param 2   //remains same
param 3   //remains same
param 4   //remains same
param 5   //remains same
ReferenceData // .csv File - is different for each test. there are total five

My confusion is:

1) whether parametrized jUnit suits for this scenario ? if yes, could some please provide little guideline how should I implement it ? #

2)if jUnit doesnt suit for this scenario then what else I can use ?

3) Should I read these parameters from .properties file in setUp method of the junit test ? is it a good practice ?


Solution

  • You can achieve this using JUnitParams lib.

    Place your xml and csv files into /src/test/resources folder of your project (valid for maven/gradle projects).

    And use them in test like this:

    @RunWith(JUnitParamsRunner.class)
    public class ServiceTest {
    
        @Test
        @Parameters({
                "first.xml, first.csv",
                "second.xml, second.csv",
                "third.xml, third.csv"
        })
        public void shouldServe(String xmlFilePath, String csvFilePath) {
            String xmlFileContent = readContent(xmlFilePath);
            String csvFileContent = readContent(csvFilePath);
    
            // call your business method here passing 
            // dynamic xml, csv and static params
        }
    }
    

    Where readContent is method that reads content from text files.