简体   繁体   中英

I have a Junit4 test with a RuntimeException "should have no parameters" for a parameterized test

I'm running some code using Junit4 and an external jar of JunitParams.

As far as I could tell, my code was all set right, and imports are all correct...but my method under test keeps throwing runtime exceptions since it seems to ignore the @Parameters annotation hanging out in it.

When it reaches my method under test, it keeps saying "java.lang.Exception: Method ----- should have no parameters"

I'm not sure what the issue is, even though other people have run into issues with mix/matched import statements, etc. I checked mine and that doesn't seem like the case here.

Test Runner class:

package Chapter8.Wrappers.Tests;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
    public static void main(String[] args){

        Result result = JUnitCore.runClasses(StringProcessorTest.class);

        for(Failure failures: result.getFailures()) {
            System.out.println(failures.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}

and my test class, StringProcessorTest:

package Chapter8.Wrappers.Tests;

import Chapter8.Wrappers.Challenges.BackwardsString.StringProcessor;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
import java.util.Collection;

@RunWith(JUnitParamsRunner.class)
public class StringProcessorTest {

    private StringProcessor processor;
    private final String expectedResult;
    private final String inputString;

    public StringProcessorTest(String inputString, String expectedResult){
        super();
        this.inputString = inputString;
        this.expectedResult = expectedResult;
    }

    @Before
    public void initialize(){
        processor = new StringProcessor();
    }

    @Parameters
    public static Collection stringValues(){
        return Arrays.asList(new Object[][] { {"HELLO","OLLEH"},
                {"TESTING", "GNITSET"} });
    }

    @Test
    public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult){

        Object input;

        input = inputString;

        processor = new StringProcessor(input);

        Assert.assertEquals(expectedResult, processor.printReverseString());

    }
}

Console output:

Chapter8.Wrappers.Tests.StringProcessorTest: java.lang.Exception: Method objectConstructor_InitializeStringBuilderInside_EqualsPassedString should have no parameters
false

Process finished with exit code 0

I found something that ended up working out- I changed the inside of the @RunWith annotation to

Parameterized.class

It turned out I was trying to pass parameters into the method under the @Test annotation, and not just the constructor of my StringProcessorTest class; I removed them from the method under test. It was already warning me about that but I didn't understand why earlier.

When I re-ran it, it completed without issues and ran both times.

I think you have two choices to resolve your issue:

  1. Remove the @Parameters annotation from stringValues() method and add it to your test method like this:
private List stringValues(){
    return Arrays.asList(new Object[][] { {"HELLO","OLLEH"}, {"TESTING", "GNITSET"} });
}

@Test
@Parameters(method = "stringValues")
public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult) {
    processor = new StringProcessor(inputString);
    
    Assert.assertEquals(expectedResult, processor.printReverseString());
}

OR

  1. Remove stringValues() method and put the @Parameters annotation directly on your test method:
@Test
@Parameters({ "HELLO, OLLEH", "TESTING, GNITSET" })
public void objectConstructor_InitializeStringBuilderInside_EqualsPassedString(String inputString, String expectedResult) {
    processor = new StringProcessor(inputString);
    
    Assert.assertEquals(expectedResult, processor.printReverseString());
}

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