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());
}

然后我得到以下异常:

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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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

我还没有在网上找到相关的...

我的整个班级:

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());
}

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

@RunWith(JUnitParamsRunner.class)
public class TestClass {

您还需要导入

import junitparams.JUnitParamsRunner;

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

我在寻找类似问题的问题时偶然发现了这篇文章。 我收到这样的异常:

java.lang.Exception:方法 x 应该没有参数

事实证明,我对@Test注释使用了错误的导入。我最初使用import org.junit.Test;但我需要使用的是import org.junit.jupiter.api.Test;

长话短说,确保您正在检查导入,有时自动生成的导入并不是您想要的。

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

对于由于相同的错误而来到这里的其他人,他们看到的不是JUitParameter。

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

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)

该错误是因为对于带参数的方法,我不小心添加了@Test标签。(当我测试该方法时添加了它,然后忘记删除它)。傻我。当我删除@Test时工作正常。如果你想要@Test,那么该方法不应该有任何参数

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

导入需要更新,如下所示:

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

现在它工作了

如果您

使用的是JMockit,则添加以下代码片段可以解决问题

import mockit.integration.junit4.JMockit;
@RunWith(JMockit.class)
public class TestClass

相关内容

  • 没有找到相关文章

最新更新