1

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

我正在使用参数化来测试各种输入。我使用 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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:config/applicationContext-http-int.xml",
    "classpath:config/web-application-config.xml" })

public class TestValidatePolicy extends BaseTest{

    @Autowired
    private Service Service;

    private String Number;
    private String Code;    

        @Parameters
    public static Collection primeNumbers() {
      return Arrays.asList(new Object[][] {
         { "8001060001045", "86502" },
         { "8001060001039", "85902" },

      });
   }

   public TestValidatePolicy(String Number,String Code){

    this.Number = Number;
    this.Code = Code;       
   }    

   @Test    
   public void testValidatePolicyDetails() throws Exception {

    String xmlValue = getStringFromStream("validatePolicy.xml");        
    Assert.assertEquals(xmlValue, Service.getDetails(Number,Code));
   }    
}
4

1 回答 1

2

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

 public TestValidatePolicy(String Number,String Code){
    ...
 }

错误消息说(正确):

 Test class should have exactly one public zero-argument constructor

要么显式创建一个零参数构造函数:

 public TestValidatePolicy() {
    ...
 }

...或摆脱两个参数的构造函数。

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

@RunWith(Parameterized.class)

...但是您已将其替换为 Spring runner:

@RunWith(SpringJUnit4ClassRunner.class)

您不能有两个 @RunWith 注释,并且(据我所知)没有ParameterizedSpringJUnit4ClassRunner. 那么该怎么办?

我在这里找到了一个建议。

利用:

 @RunWith(Parameterized.class)

由于您不能使用 Spring 运行器,因此您需要手动设置相同的行为:

@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() {
    ...
}
于 2014-02-19T12:16:44.623 回答