以非静态方式创建JUnit TestSuite
我正在寻找一种以非静态方式创建并运行 JUnit TestSuite 的方法。
目前我正在做这样的事情:
public class MyTestSuite {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTest(...);
suite.addTest(...);
// ....
return suite;
}
}
我这样做是因为我正在创建以编程方式添加到套件中的测试用例。 通过这个解决方案,我面临着我的类 MyTestSuite 从未实例化的问题。我想将它与 spring 容器连接,例如使用
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={...})
@Transactional
,但我看不出有什么办法告诉 SpringJUnit4ClassRunner 它也应该执行我的编程测试。
感谢您的帮助! 埃里克
I am seeking for a way to create and let run a JUnit TestSuite in a non-static fashion.
Currently I am doing something like this:
public class MyTestSuite {
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTest(...);
suite.addTest(...);
// ....
return suite;
}
}
I am doing this because I am creating the TestCases I am adding to the suite programmatically.
With this solution I am facing the problem that my class MyTestSuite is never instantiated. I would like to wire it with a spring container, e.g. using
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={...})
@Transactional
but I see no way of telling the SpringJUnit4ClassRunner that it should also execute my programmatic tests.
Thanks for your help!
Erik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么要使用套房?将您的测试放在自己的子目录中并使用 ant (或您正在使用的任何构建工具)目标来运行仅在那里找到的测试似乎更简单。
Why use a suite at all? Seems simpler to put your tests in their own subdirectory and have an ant (or whatever build tool you're using) target that runs just the tests found there.
您可以尝试将 MyTestSuite 作为 spring 上下文(测试上下文)的一部分,并在其上触发一个 init 方法,该方法将添加您的编程测试。这将允许您注入 MyTestSuite,它在由 spring 实例化时添加了此程序测试。
希望有帮助。
You could try and have MyTestSuite as part of your spring context (the test context) and fire an init method on it which would add your programmatic tests. That would allow you to inject MyTestSuite which has this programmtic tests added when it is instantiated by spring.
Hope that helps.
对于 JUnit3 风格的
suite
方法,JUnit 不会创建该类的实例;它调用该方法并对返回的对象调用run(TestResult)
。SpringJUnit4ClassRunner
是一个 JUnit4 Runner 类,因此它不能用于影响 JUnit3 风格的测试套件的行为。 Spring 不提供 JUnit4 风格的套件实现。如果您希望每个测试用例都使用 SpringJUnit4ClassRunner,最好的选择是将它们升级到 JUnit4。如果您询问如何将 Spring 测试添加到
MyTestSuite
:For JUnit3-style
suite
methods, JUnit does not create an instance of the class; it calls the method and callsrun(TestResult)
on the returned object.SpringJUnit4ClassRunner
is a JUnit4 Runner class, so it cannot be used to affect the behavior of JUnit3-style test suites. Spring does not provide a JUnit4-style suite implementation. If you want each of the test cases to useSpringJUnit4ClassRunner
, your best option is to upgrade them to JUnit4.If you are asking how you add your Spring tests to
MyTestSuite
: