如何注入参数到TestNG类的构造函数? [英] How to inject parameter into constructor of TestNG class?
问题描述
我已经实现了一个具有策略模式的程序。所以我有一个接口,在一些地方使用,具体的实现可以替换。
现在我想测试这个程序。我想以类似的方式做。编写一次测试,对接口进行测试。
我的测试类看起来和这个类似:
public class MyTestClass {
private StrategeyInterface strategy;
public MyTestClass(StrategeyInterface strategy){
this.strategy = strategy;
}
....使用策略的测试方法。
}
参数化构造器必须用于注入具体策略实现。测试。
现在我没有得到TestNG运行它并注入具体的实现实例。我试过几种方法与继承, @DataProvider
, @Factory
和相应的方法,但没有运气。 >
这里是testNG报告说的:
无法调用public void MyClass.myTestMethod():使它静态或添加一个无参数的构造函数到你的类
使用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>
如何编写和运行测试,
提前感谢。
我可以提供更多的代码,我试过。我没有把它放在这里,但是,因为我尝试这么多变种,所以我现在困惑,所有的失败。
如果没有,您可以使用工厂和数据提供者的混合:
@Factory(dataProvider =dp)
public FactoryDataProviderSampleTest(StrategyInterface si){
}
$ b b @DataProvider
static public Object [] [] dp(){
return new Object [] [] {
new Object [] {new Strategy1Impl()},
new Object [] {new Strategy2Impl()},
};
}
I have implemented a programm with the strategy pattern. So I have an interface which is used at some places and the concrete implementation may be replaced.
Now I want to test this programm. I would like to do it in a similar way. Write a test once, which tests against the interface. The concrete interface implementation should be injected at the beginning of the test, so that I may replace it easily.
My testclass looks similar to this one:
public class MyTestClass {
private StrategeyInterface strategy;
public MyTestClass(StrategeyInterface strategy) {
this.strategy = strategy;
}
....test methods using the strategy.
}
The parameterized contructor must used to be inject the concrete strategy implementation at thr beginning og the tests.
Now I did not get TestNG to run it and inject the concrete implementation instance. I tried several ways with inheritance, @DataProvider
, @Factory
and the corresponding methods, but without luck.
Here is what the testNG report says:
Can't invoke public void MyClass.myTestMethod(): either make it static or add a no-args constructor to your class
I use the maven surefire plugin to run the tests. Here is the relevant part of the 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>
How do I write and run the tests, with injecting a concrete implementation into the test class?
Thanks in advance.
P.S. I could deliver more code I tried. I did not post it here, yet, because I tried so many variants so that I am kind of confused now and all of them fail.
You have several options. If you are using Guice, here is a very straightforward way to inject your implementation.
If not, you can use a mix of factories and data provider:
@Factory(dataProvider = "dp")
public FactoryDataProviderSampleTest(StrategyInterface si) {
}
@DataProvider
static public Object[][] dp() {
return new Object[][] {
new Object[] { new Strategy1Impl() },
new Object[] { new Strategy2Impl() },
};
}
这篇关于如何注入参数到TestNG类的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!