1

This is my test class.When I try to run the test method am getting following exception.

I am using parmeterized for testing for various inputs.Am using maven.not using ant build.

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));
   }    
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
Renganathan V
  • 413
  • 1
  • 9
  • 27

1 Answers1

2

Your test class has a constructor with two arguments:

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

The error message says (correctly):

 Test class should have exactly one public zero-argument constructor

Either create a zero-argument constructor explicitly:

 public TestValidatePolicy() {
    ...
 }

... or get rid of the two-argument constructor.

-- but you're trying to run parameterized jUnit4 tests. These require an @RunWith to specify a Parameterized runner:

@RunWith(Parameterized.class)

... but you've replaced that with a Spring runner:

@RunWith(SpringJUnit4ClassRunner.class)

You can't have two @RunWith annotations, and (as far as I know) there is no ParameterizedSpringJUnit4ClassRunner. So, what to do?

I found a suggestion here.

Use:

 @RunWith(Parameterized.class)

Since you can't use the Spring runner, you need to manually set up the same behaviour:

@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() {
    ...
}
slim
  • 40,215
  • 13
  • 94
  • 127