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

  • Home
  • SEARCH
  • 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 6869159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:31:24+00:00 2026-05-27T03:31:24+00:00

Is there a way to know in a JUnit 4 test Class, if the

  • 0

Is there a way to know in a JUnit 4 test Class, if the Class was initiated by a Test-Suite ?
I have global things that I want to run before all tests (regarding in-memory DB), so I thought doing it in the test-suit. However, I still want to be able to initiate one test at a time without a Test-Suit, So I need to know if I need to initialize the global things in the @Before part of the test… Does any-one know if it’s 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-05-27T03:31:24+00:00Added an answer on May 27, 2026 at 3:31 am

    There are several ways to achieve this. The easiest and simplest is to have a ‘test’ which is run at the beginning and end of your suite which set up your database and then set a global flag. In your @Before and @After tests you check this flag and if necessary do the setup/teardown.

    @RunWith(Suite.class)
    @SuiteClasses({SetupTest.class, RealTest.class, TeardownTest.class});
    

    This is the simplest solution, but it isn’t very nice, so a neater solution would be to use a TestRule. Look at extending ExternalResource. This implements before & after logic which surrounds your test methods. This would allow you to factor out your @Before and @After methods, to reuse the same code everywhere.

    Then, for your suite, you need to implement before/after logic as well. Unfortunately, the class annotated with @RunWith(Suite.class) isn’t actually instantiated, so you can’t use the constructor of that class, but you can extend Suite. Depending upon how you will run this, you will will need to implement one of the constructors, using @RunWith as an example:

    public class MySuite extends Suite {
        /**
         * Called reflectively on classes annotated with <code>@RunWith(Suite.class)</code>
         * 
         * @param klass the root class
         * @param builder builds runners for classes in the suite
         * @throws InitializationError
         */
        public MySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
            this(builder, klass, getAnnotatedClasses(klass));
            // put your global setup here
            // set global variable
        }
    }
    

    Then run your test suite with

    @RunWith(MySuite.class)
    

    There are several constructors which are used in different situations, look at the comments next to each for specifics. You still need to use a global variable so that your Rules don’t re-execute the setup code. The above will work if you’re wanting to execute only setup code, executing teardown code is harder, but can be done. Let me know if you need it 🙂

    If you’re wanting more flexibility (say executing setup code only for specific methods), then see my answer to How to define JUnit method rule in a suite?.

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

Sidebar

Related Questions

Is there a way to know the number of test methods in a test
Is there any way to know which version of MS-Office I have on my
Is there any way to know the type of the FileStream . I have
Using the JUnit assumeTrue, you can skip a test, but is there any way
Is there a way to know when the dismissModalViewControllerAnimated is initiated and when it
I work with Maven2 . I want to know is there a way to
Is there any way to know if I'm compiling under a specific Microsoft Visual
Is there a way to know the main/calling request in an httpmodule? I only
Is there a way to know and output the stack size needed by a
Is there a way to know if Visual C++ 2008 SP1 Redistributable Package is

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.