supernova
supernova

Reputation: 3271

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?

Upvotes: 5

Views: 6569

Answers (3)

sajux
sajux

Reputation: 136

Use the @Ignore on each method whom you do not want to execute. Taking your example:

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


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


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

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

All methods marked as @Ignore will not be executed

Upvotes: 0

artbristol
artbristol

Reputation: 32437

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

Upvotes: 1

Stijn Geukens
Stijn Geukens

Reputation: 15628

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

Upvotes: 1

Related Questions