How to enter a parameter in the constructor of the TestNG class?

I implemented a program with a strategy template. Therefore, I have an interface that is used in some places, and the specific implementation can be replaced.

Now I want to test this program. I would like to do it in a similar way. Write a test once that tests the interface. An implementation of a specific interface should be introduced at the beginning of the test so that I can easily replace it.

My test class is similar to this:

public class MyTestClass {

    private StrategeyInterface strategy;

    public MyTestClass(StrategeyInterface strategy) {
        this.strategy = strategy;
    }
    ....test methods using the strategy.
}

A parameterized constructor should be used to implement the implementation of a specific strategy at the beginning of testing.

Now I did not get TestNG to run it and entered a specific implementation instance. I have tried several ways with inheritance @DataProvider, @Factoryand the appropriate methods, but with no luck.

testNG:

Can't invoke public void MyClass.myTestMethod(): either make it static or add a no-args constructor to your class

maven surefire . pom.xml:

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

, ?

.

P.S. , . , , , .

+1
1

. Guice, .

, :

@Factory(dataProvider = "dp")
public FactoryDataProviderSampleTest(StrategyInterface si) {
}

@DataProvider
static public Object[][] dp() {
  return new Object[][] {
    new Object[] { new Strategy1Impl() },
    new Object[] { new Strategy2Impl() },
  };
}
+4

All Articles