Spring 3+ How to create TestSuite when JUnit does not recognize it

I am using Spring 3.0.4 and JUnit 4.5. My test classes currently use Spring tag 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 really don't need the TestCase extends string to run this test. This was not needed when running this test class. I had to add extends TestCase to add it to the TestSuite class:

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

If I omit extends TestCase , my test suite will not start. Eclipse will mark suite.addTestSuite (MyAppTest.class) as an error.

How to add test kit Spring 3+? I am sure there is a better way. I am GOOGLED and read the docs. If you do not believe me, I am ready to send you all my bookmarks as evidence. But in any case, I would prefer a constructive answer. Many thanks.

+6
spring junit testing
source share
1 answer

You're right; Tests like JUnit4 should not extend junit.framework.TestCase

You can enable the JUnit4 test as part of the JUnit3 package as follows:

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

Usually you add this method to the MyAppTest class. You can then add this test to your larger package:

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

You can create a package in JUnit4 style by creating a class annotated with Suite

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

Note that AccountTest can be a JUnit4 style test or a JUnit3 style test.

+6
source share

All Articles