0

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

CoderBeginner
  • 717
  • 1
  • 12
  • 39

1 Answers1

1

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

spehler62
  • 46
  • 5
  • I tried this with @before and my condition is returning false for the testclasses I don't want to execute. So, i am getting an error org.junit.internal.AssumptionViolatedException: got: , expected: is in console. Is this the correct way.? – CoderBeginner Jul 28 '20 at 13:35
  • Yes, this should be correct. It executes everything before it comes to the assumption. But in the execution stats of JUnit it should show ignored instead of failed. – spehler62 Jul 28 '20 at 13:46
  • Yes, it is showing ignored. I was only concerned about the AssumptionViolatedException thrown at console. If it could be handled in a better way – CoderBeginner Jul 28 '20 at 14:24
  • There is an older Stackover question similar to your question. Have you checked it out [Conditionally ignoring tests in JUnit 4](https://stackoverflow.com/questions/1689242/conditionally-ignoring-tests-in-junit-4?noredirect=1&lq=1) – spehler62 Jul 28 '20 at 14:42