Spring junit异常

Spring junit异常,junit,parameterized,spring-junit,Junit,Parameterized,Spring Junit,这是我的测试类。当我尝试运行该测试方法时,出现以下异常 我使用parmeterized测试各种输入。我使用maven。而不是使用ant构建 Exception: java.lang.Exception: Test class should have exactly one public zero-argument constructor at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(Block

这是我的测试类。当我尝试运行该测试方法时,出现以下异常

我使用parmeterized测试各种输入。我使用maven。而不是使用ant构建

Exception:
java.lang.Exception: Test class should have exactly one public zero-argument constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:147)
    at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:124)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:104)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass

您的测试类有一个具有两个参数的构造函数:

 public TestValidatePolicy(String Number,String Code){
    ...
 }
错误消息显示(正确):

显式创建零参数构造函数:

 public TestValidatePolicy() {
    ...
 }
。。。或者去掉双参数构造函数

--但您正在尝试运行参数化jUnit4测试。这些需要@RunWith来指定参数化的运行程序:

@RunWith(Parameterized.class)
。。。但你已经用Spring runner取代了它:

@RunWith(SpringJUnit4ClassRunner.class)
您不能有两个@RunWith注释,而且(据我所知)没有
参数化SpringJUnit4ClassRunner
。那么,该怎么办

我找到了一个建议

使用:

由于无法使用Spring runner,因此需要手动设置相同的行为:

@RunWith(Parameterized.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class EmailTest {

private TestContextManager testContextManager;

@Before
public void setUpContext() throws Exception {
    //this is where the magic happens, we actually do "by hand" what the spring runner would do for us,
    // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though
    this.testContextManager = new TestContextManager(getClass());
    this.testContextManager.prepareTestInstance(this);
}

@Parameters
public static Collection<object []> data() {
    ...
}
@RunWith(参数化的.class)
@ContextConfiguration(位置={“classpath:applicationContext.xml”})
公共类电子邮件测试{
私有TestContextManager TestContextManager;
@以前
public void setUpContext()引发异常{
//这就是神奇发生的地方,我们实际上是“用手”做春跑者会为我们做的事,
//阅读下面类的JavaDoc以确切了解它的功能,尽管方法名称非常准确
this.testContextManager=新的testContextManager(getClass());
this.testContextManager.prepareTestInstance(this);
}
@参数
公共静态收集数据(){
...
}

我添加了zero-arg构造函数,但它无法解决问题。这实际上是参数化的junit..因此需要像这样的构造函数进行更新以适应。
@RunWith(SpringJUnit4ClassRunner.class)
 @RunWith(Parameterized.class)
@RunWith(Parameterized.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class EmailTest {

private TestContextManager testContextManager;

@Before
public void setUpContext() throws Exception {
    //this is where the magic happens, we actually do "by hand" what the spring runner would do for us,
    // read the JavaDoc for the class bellow to know exactly what it does, the method names are quite accurate though
    this.testContextManager = new TestContextManager(getClass());
    this.testContextManager.prepareTestInstance(this);
}

@Parameters
public static Collection<object []> data() {
    ...
}