java junitparameter异常方法应该没有参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37424725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:29:33  来源:igfitidea点击:

junitparameter exception method should have no parameter

javaexceptionjunitparams

提问by Patrick Lec

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

通过使用 JUnitParameter 测试方法,我有一个例外。我的代码类似于 JUnitParameter 上的很多示例:

    private Object parametersForTestSetDistanceFormated() {
    return new Object[][]{    
        {100,   "_1,__ km"}, 
        {100,   "1_,__ km"}, 
        {1100,  "11,__ km"}, 
        {110,   "1_,1_ km"}, 
        {111,   "1_,11 km"}};
}
/**
 * Test of setDistanceFormated method, of class ClientFormated.
 */
@Test
@Parameters
public void testSetDistanceFormated(final int exptDist, final String  distFormated) {
    System.out.println("setDistanceFormated");
    ClientFormated instance = new ClientFormated();             

    instance.setDistanceFormated(distFormated);
    assertEquals(exptDist, (long) instance.getDistance());
}

and then I obtain the following exception:

然后我得到以下异常:

java.lang.RuntimeException: java.lang.Exception: Method testSetDistanceFormated should have no parameters
    at junitparams.JUnitParamsRunner.verifyMethodCanBeRunByStandardRunner(JUnitParamsRunner.java:429)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:420)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:386)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access
import com.entities.Client;
import junitparams.JUnitParamsRunner;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;

@RunWith(JUnitParamsRunner.class)
public class ClientFormatedTest {

private Object parametersForTestSetDistanceFormated() {
    return new Object[][]{    
        {100,   "_1,__ km"}, 
        {100,   "1_,__ km"}, 
        {1100,  "11,__ km"}, 
        {110,   "1_,1_ km"}, 
        {111,   "1_,11 km"}};
}

/**
 * Test of setDistanceFormated method, of class ClientFormated.
 */
@Test
@Parameters
public void testSetDistanceFormated(final int exptDist, final String  distFormated) {
    System.out.println("setDistanceFormated");
    ClientFormated instance = new ClientFormated();             

    instance.setDistanceFormated(distFormated);
    assertEquals(exptDist, (long) instance.getDistance());
}
0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) Caused by: java.lang.Exception: Method testSetDistanceFormated should have no parameters at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:76) at junitparams.JUnitParamsRunner.verifyMethodCanBeRunByStandardRunner(JUnitParamsRunner.java:427) ... 20 more

I haven't found related on the web...

网上没找到相关的。。。

My whole classe:

我的全班:

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

采纳答案by Patrick Lec

Imports need to be updated like :

进口需要更新,如:

@RunWith(JUnitParamsRunner.class)
public class TestClass {

Now it works

现在它起作用了

回答by Vaibhav Jain

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

在类上方添加@RunWith(JUnitParamsRunner.class) 注释作为

import junitparams.JUnitParamsRunner;

Also you need to import

您还需要导入

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)

回答by Adam Michalik

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

您使用了错误的导入@Parameters- 应该是junitparams.Parameters,而不是org.junit.runners.Parameterized.Parameters

回答by AJC

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

当您的测试方法(具有 @Test 注释)有任何参数时,可能会出现此错误

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

对于因为同样的错误而来到这里的其他人,他们看到了但不是 JUitParameter。

I hit this same error, in my code.

我在我的代码中遇到了同样的错误。

@Test
void assertResult(String result) {
    Assert.assertTrue(!"403".equals(result) && !"404".equals(result) && !"400".equals(result));
    Assert.assertNotNull(result);
} 

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 标记。(在我只测试该方法时添加它,然后忘记删除它)。傻我。当我删除@Test 时工作正常。 如果你想要@Test,那么该方法不应该有任何参数

##代码##