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

5
  • Is junit5 an option? I would be a lot easier. Commented Jul 28, 2020 at 13:13
  • We are using spring 3, is junit5 compatible with it.? Commented Jul 28, 2020 at 13:19
  • also we are using java7, i guess junit5 won't work then.? Commented Jul 28, 2020 at 13:27
  • No. (you mean Spring3 from 2009? sorry) it's not "built-in" compatible with Spring3, and not with java 7 So another option is to use either Rules or assumethat. But you should at least upgrade to java 8 ! Commented Jul 28, 2020 at 13:34
  • Right, we are working on upgrading to java8. But as the code base is huge. So, it will take some time. But yes, we will surely do this Commented Jul 28, 2020 at 13:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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: <false>, expected: is <true> in console. Is this the correct way.?
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.
Yes, it is showing ignored. I was only concerned about the AssumptionViolatedException thrown at console. If it could be handled in a better way
There is an older Stackover question similar to your question. Have you checked it out Conditionally ignoring tests in JUnit 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.