junitparameter exception method should have no parameter

前端 未结 6 783
陌清茗
陌清茗 2021-01-02 05:21

By using JUnitParameter for testing a methode I have an exception. My code is similar to lot of example on JUnitParameter:

    private Object parametersForTe         


        
相关标签:
6条回答
  • 2021-01-02 05:42

    I stumbled across this article while looking for an issue to a similar problem. I was getting an exception like this:

    java.lang.Exception: Method x should have no parameters

    It turns out I was using the wrong import for the @Test annotation. I originally used import org.junit.Test; but what I needed to use was import org.junit.jupiter.api.Test;

    Long story short, make sure you are checking your imports, sometimes the auto-generated ones aren't really what you want.

    0 讨论(0)
  • 2021-01-02 05:43

    Imports need to be updated like :

    import com.entities.Client;
    import junitparams.JUnitParamsRunner;
    import junitparams.Parameters;
    import static org.junit.Assert.*;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    

    Now it works

    0 讨论(0)
  • 2021-01-02 05:52

    This error may show up when your test method(having @Test annotation) have any arguments

    For anyone else who came here because of the same error they are seeing but not with JUitParameter.

    I hit this same error, in my code.

    java.lang.Exception: Method assertResult() should be public
    
    
    at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:96)
    at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:74)
    at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:155)
    at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRunner.java:208)
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:188)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    

    The error was because for a method with argument, I had accidentally added a @Test tag. (Added it when I was testing just that method and then forgot to remove it). Silly me. Worked fine when I removed the @Test. If you want the @Test, then the method shouldn't have any arguments

    @Test
    void assertResult(String result) {
        Assert.assertTrue(!"403".equals(result) && !"404".equals(result) && !"400".equals(result));
        Assert.assertNotNull(result);
    } 
    
    0 讨论(0)
  • 2021-01-02 06:00

    You are using wrong import for @Parameters - it should be junitparams.Parameters, not org.junit.runners.Parameterized.Parameters

    0 讨论(0)
  • 2021-01-02 06:05

    Add @RunWith(JUnitParamsRunner.class) annotation above class as

    @RunWith(JUnitParamsRunner.class)
    public class TestClass {
    

    Also you need to import

    import junitparams.JUnitParamsRunner;
    
    0 讨论(0)
  • 2021-01-02 06:08

    If you are using JMockit, then adding below snippet resolves the issue

    import mockit.integration.junit4.JMockit;
    
    @RunWith(JMockit.class)
    public class TestClass
    
    0 讨论(0)
提交回复
热议问题