如何将参数注入TestNG类的构造函数?
我已经实施了一个具有策略模式的程序。所以我有一个在某些地方使用的接口,并且具体实现可能会被替换。
现在我想测试这个程序。我想以类似的方式来做。编写一次测试,针对接口进行测试。具体的接口实现应该在测试开始时注入,这样我就可以轻松替换它。
我的测试类看起来与此类似:
public class MyTestClass {
private StrategeyInterface strategy;
public MyTestClass(StrategeyInterface strategy) {
this.strategy = strategy;
}
....test methods using the strategy.
}
必须使用参数化构造函数在测试开始时注入具体策略实现。
现在我没有让 TestNG 运行它并注入具体的实现实例。我尝试了多种继承、@DataProvider、@Factory 和相应方法的方法,但没有成功。
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>
如何编写和运行测试,并将具体实现注入到测试类中?
提前致谢。
PS我可以提供更多我尝试过的代码。我还没有将其发布在这里,因为我尝试了很多变体,所以我现在有点困惑,而且所有变体都失败了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有多种选择。如果您使用 Guice,这是注入实现的非常简单的方法。
如果没有,您可以混合使用工厂和数据提供程序:
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: