Search code examples
javaspringunit-testingmockitojunit4

How to execute test classes conditionally using Junit 4.11


I have multiple test classes and I want to execute test classes based on a bean value.

My test class:

@autowired
protected String abType;

public  class abTest extends TestAbstract {

@Test
public void testAddUser() {
---------
--------
--------
}

I want this class or its test cases to execute only when abType = a;

TestAbstract class :

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = "classpath:application-context-test.xml")
public abstract class TestAbstract {

 @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }
.
.
.
.
.
}

This class is extended by all the test classes, I want to run all the test classes conditionally based on the beanValue which is configured in config.properties file.

I read multiple posts related to this, but didn't got what I am actually looking for. Any help would be really appreciated.!!


Solution

  • I usually use the assumeTrue in JUnit4. Maybe this is an option for you.

    @Test
    public void testOnlyWhenConditionTrue() {
       assumeTrue(conditionTrue);
       ... // your test steps
    }
    

    Assumptions and Conditional Test Execution with JUnit 4 and 5