Kishore Jawalkar
Kishore Jawalkar

Reputation: 21

Error "Test class should have exactly one public zero-argument constructor" is returned

after searching and finding no proper answer, i am raising it here.

I wrote the below simple class with a @Test and when i try to run i am getting "java.lang.Exception: Test class should have exactly one public zero-argument constructor" error. Can someone please help me in resolving this issue.

package Shopping.Tests;

import Shopping.Config.TestData;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class TestScenario1 {

private final TestData testData;

public TestScenario1(TestData testData) {
    this.testData = testData;
}

@Test
public void sampleTest(){
    log.info(testData.getBaseURL());
}
}

Upvotes: 1

Views: 10242

Answers (1)

ernest_k
ernest_k

Reputation: 45319

This non-default constructor makes it impossible for the testing framework to create an instance of the class

public TestScenario1(TestData testData) {

You need a no-arg constructor:

public TestScenario1() { //or have no explicit constructor declared at all

Initializing test data is typically done in a method annotated with @Before (assuming junit is your test framework - see @Before and @BeforeClass)

public TestScenario1() {}
@Before
public void prepareTestData() {
    this.testData = fetchTestData()...//It can't be passed in as parameter.
}

Upvotes: 4

Related Questions