本文介绍了Spring 3+如何在JUnit无法识别时创建TestSuite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 3.0.4和JUnit 4.5.我的测试类当前使用具有以下语法的Spring注释测试支持:

I'm using Spring 3.0.4 and JUnit 4.5. My test classes currently uses Spring's annotation test support with the following syntax:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = { "classpath:configTest.xml" })
@TransactionConfiguration (transactionManager = "txManager", defaultRollback = true)
@Transactional
public class MyAppTest extends TestCase

{
 @Autowired
 @Qualifier("myAppDAO")
 private IAppDao appDAO;
    ...
}

我真的不需要行扩展TestCase 来运行此测试.单独运行此测试类时不需要.我必须添加扩展TestCase ,以便可以将其添加到TestSuite类中:

I don't really need the line extends TestCase to run this test. It's not needed when running this test class by itself. I had to add extends TestCase so that I can add it in a TestSuite class:

public static Test suite() {
        TestSuite suite = new TestSuite("Test for app.dao");
  //$JUnit-BEGIN$
  suite.addTestSuite(MyAppTest.class);
        ...

如果我省略了扩展TestCase ,我的测试套件将无法运行. Eclipse会将 suite.addTestSuite(MyAppTest.class)标记为错误.

If I omit the extends TestCase, my Test Suite will not run. Eclipse will flag suite.addTestSuite(MyAppTest.class) as error.

如何将Spring 3+测试类添加到测试套件中?我敢肯定有更好的方法.我已经使用GOOGLED并阅读了文档.如果您不相信我,我愿意将您所有的书签发送给您作为证明.但是无论如何,我都希望有一个建设性的答案.非常感谢.

How do I add a Spring 3+ test class to a Test Suite? I'm sure there's a better way. I've GOOGLED and read the docs. If you don't believe me, I'm willing to send you all my bookmarks as proof. But in any case, I would prefer a constructive answer. Thanks a lot.

推荐答案

您是对的; JUnit4样式的测试不应扩展junit.framework.TestCase

You are right; JUnit4-style tests should not extend junit.framework.TestCase

您可以通过以下方式将JUnit4测试作为JUnit3套件的一部分包括在内:

You can include a JUnit4 test as part of a JUnit3 suite this way:

public static Test suite() {
   return new JUnit4TestAdapter(MyAppTest.class);
}

通常,您需要将此方法添加到MyAppTest类中.然后,您可以将此测试添加到较大的套件中:

Usually you would add this method to the MyAppTest class. You could then add this test to your larger suite:

 public class AllTests {
   public static Test suite() {
     TestSuite suite = new TestSuite("AllTests");
     suite.addTest(MyAppTest.suite());
     ...
     return suite;
   }
}

您可以通过创建一个用套房

You can create a JUnit4-style suite by creating a class annotated with Suite

@RunWith(Suite.class)
@SuiteClasses( { AccountTest.class, MyAppTest.class })
public class SpringTests {}

请注意,AccountTest可以是JUnit4样式的测试,也可以是JUnit3样式的测试.

Note that AccountTest could be a JUnit4-style test or a JUnit3-style test.

这篇关于Spring 3+如何在JUnit无法识别时创建TestSuite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:51