Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 475
chris Avatar asked Sep 11 '10 14:09

chris


1 Answers

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.

like image 166
NamshubWriter Avatar answered Oct 11 '22 15:10

NamshubWriter