2014-02-19 120 views
1

這是我的測試類。當我嘗試運行測試方法時遇到以下異常。Spring junit異常

我使用parmeterized測試各種inputs.Am使用maven.not使用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)); 
    }  
} 

回答

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亞軍:

@RunWith(SpringJUnit4ClassRunner.class) 

你不能有兩個@RunWith註釋和(據我所知)沒有ParameterizedSpringJUnit4ClassRunner。那麼該怎麼辦?

我找到了一個建議here

用途:

@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() { 
    ... 
} 
+0

我加零ARG construcotor但不力解決問題。 –

+0

我增加了零arg construcotor,但它的力量解決了這個問題。這實際上是參數化junit ..所以需要有這樣的構造函數 –

+0

啊哈;更新以適應。 – slim