Spring 3+ How to create a TestSuite when JUnit is not recognizing it

蓝咒 提交于 2019-12-04 23:30:42

问题


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;
    ...
}

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);
        ...

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

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.


回答1:


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

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

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

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 {}

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



来源:https://stackoverflow.com/questions/3691361/spring-3-how-to-create-a-testsuite-when-junit-is-not-recognizing-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!