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 6907619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:27:29+00:00 2026-05-27T08:27:29+00:00

I have a java package which contains all my test case classes. Each class

  • 0

I have a java package which contains all my test case classes. Each class contains a lot of test cases. Each class is loaded and run one by one by JUnit. However each of the classes contains common configuration code which is run again and again and initialised everytime each of the classes are run.
These initializations take a lot of time.

Is there some way to load these configuration changes first and then run the test case so that I do not need to load them everytime.

  • 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-27T08:27:30+00:00Added an answer on May 27, 2026 at 8:27 am

    JUnit4 has @BeforeClass annotation.
    Just do something like this:

    public class TestClass {
    
      private static SomeConnection connection;
    
      @BeforeClass
      public static void setUp() {
        //do common setup
        connection = new SomeConnection();
      }
    
      @Test
      public void testSomething() { }
    
      @Test
      public void testSomethingElse() { }
    
      @AfterClass
      public static void tearDown() {
        //do teardown operations
        connection.close();
      }
    
    }
    

    Method marked with @BeforeClass will run only once. Just make sure you use JUnit4.

    Update:
    Also note, that it should be static, and as @ChristopheRoussy mentioned, you can use @AfterClass to destroy your common setup.

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

Sidebar

Related Questions

Lets say I have a java package commands which contains classes that all inherit
If I have a java class which is package-private (declared with class, not public
I have a package with logic classes(like CheckAuthenticationDataLogic.java, GetVocabulariesLogic.java). And another class - ApiService.java
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I was wondering if it's possible to have a java package setup using a
It is my understanding that the java.regex package does not have support for named
I have a Java project in Eclipse with ~10 packages and ~10 class files
I have a rather large project which contains a number of third-party dependencies which
My LocalService.java contains a class LocalBinder that defines the methods that are declared in
I have a directory structure like com/example/web under the root directory which contains a

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.