Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write spring test suite of multiple tests and run selective tests?

I have many spring test methods in a test class. i want to run only selective tests. So i want to create a test suite in same class.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/testApplicationContext.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestersChoice  { 


@Test
@Transactional
public void testAddAccount(){        
  ///do something ....
}  


@Test
@Transactional
public void testDeleteAccount(){        
  ///do something ....
}   

@Test
@Transactional
public void testReadAccount(){        
  ///do something ....
}   

}

If I run this Class TestersChoice all tests will run! I just want to run testReadAccount not the rest. I want to create suite to run selective tests. (I want to avoid deleting @Test above test methods to achieve this) Something like in jUnit testcase . This is what i was able to do by extending TestersChoice class to TestCase and inserting this method:

public static TestSuite suite(){
      TestSuite suite = new TestSuite();
       suite.addTest(new TestersChoice("testDeleteAccount"));
      return suite;
}

But as now I am not extending TestCase so I am unable to add TestersChoice instance to suite!

How to run selective tests?

like image 688
supernova Avatar asked Nov 06 '22 02:11

supernova


2 Answers

if what you want is to group your tests then the issue is not with spring-test but rather with JUnit where grouping tests is not possible. Consider switching to TestNG (also supported by spring OOTB).

TestNG is built on JUnit but a lot more powerfull: See comparison.

Grouping is easy.

regards,
Stijn

like image 168
Stijn Geukens Avatar answered Nov 09 '22 12:11

Stijn Geukens


You can use Spring's IfProfileValue (if you are always using @RunWith(SpringJUnit4ClassRunner.class)) and then tests will only run if you specify particular system properties using -D<propertyname>.

like image 30
artbristol Avatar answered Nov 09 '22 11:11

artbristol