Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:23:10+00:00 2026-06-08T11:23:10+00:00

What is the best way to enable/disable a junit test based on a Config

  • 0

What is the best way to enable/disable a junit test based on a Config parameter? Say I have a Config class which dictates some state of the software that makes a given set of tests invalid.

I could put the body of the test in an if statement within the test method, e.g.:

@Test
public void someTest() {
   if(Config.shouldIRunTheTests()) {
      //do the actual test
   }
}

This seems bad because I actually get test passes for these cases when I actually want these tests skipped.
Would like something like:

@Test[Config.shouldIRunTheTests()] 
public void someTest() {
    //do the actual test
}

Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:23:12+00:00Added an answer on June 8, 2026 at 11:23 am

    Actually I think the best solution on this case is to write your own org.junit.Runner. It is not so complicated as it seems. A simple sample would be:

    The Runner:

    package foo.bar.test;
    
    import org.junit.runner.Description;
    import org.junit.runner.Runner;
    import org.junit.runner.notification.Failure;
    import org.junit.runner.notification.RunNotifier;
    import org.junit.runners.JUnit4;
    import org.junit.runners.model.InitializationError;
    
    public class MyRunner extends Runner {
    
        private final Runner runner;
    
        public MyRunner(final Class<?> klass) throws InitializationError {
            super();
            this.runner = new JUnit4(klass);
        }
    
        @Override
        public Description getDescription() {
            return runner.getDescription();
        }
    
        @Override
        public void run(final RunNotifier notifier) {
            for (Description description : runner.getDescription().getChildren()) {
                notifier.fireTestStarted(description);
                try {
                    // here it is possible to get annotation:
                    // description.getAnnotation(annotationType)
                    if (MyConfiguration.shallExecute(description.getClassName(), description.getMethodName())) {
                        runner.run(notifier);
                    }
                } catch (Exception e) {
                    notifier.fireTestFailure(new Failure(description, e));
                }
            }
        }
    
    }
    

    The test case:

    package foo.bar.test;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    @RunWith(MyRunner.class)
    public class TestCase {
    
        @Test
        public void myTest() {
            System.out.println("executed");
        }
    
    }
    

    And configuration class:

    package foo.bar.test;
    
    public class MyConfiguration {
    
        public static boolean shallExecute(final String className, final String methodName) {
            // your configuration logic
            System.out.println(className + "." + methodName);
            return false;
        }
    
    }
    

    Here the cool thing is that you could implement your own annotation, for example: @TestKey("testWithDataBase"), see comments on the example source above. And your configuration object could define if the test should run or not, so you can group tests, what is quite useful when you have a lot of tests that needs to be grouped.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The best way to explain my problem is by a example. I have a
The best way of describing this is I have a table of people with
What is the best way to disable UITextField's 'return' keyboard key if input area
I have many projects which inherit from a parent pom. I enable Cobertura coverage
I am following this thread to disable/ enable the click on a button, Best
I have a view which contains some tabs (TabView). Initially some tabs are disabled
I have a ListView control, and I'm trying to figure out the easiest/best way
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html
The best way I think to explain this is to tell you what my
Any best way to run the jasmine HTML reporter with browserify styled code? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.