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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:03:05+00:00 2026-05-29T06:03:05+00:00

In a unit test, I need to perform a quite complex setup (this may

  • 0

In a unit test, I need to perform a quite complex setup (this may be a code smell but this is not what this question is about :-)). What I’m interested in is if it is better to have multiple @Before methods performing the setup or just one, which calls helper methods to perform the initialization.

E.g.

@Before
public void setUpClientStub() {

}

@Before
public void setUpObjectUnderTest() {

}

vs.

@Before
public void setUp() {
    setUpClientStub();
    setUpObjectUnderTest();
}
  • 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-29T06:03:06+00:00Added an answer on May 29, 2026 at 6:03 am

    As has been said in other responses, the order in which JUnit finds methods is not guaranteed, so the execution order of @Before methods can’t be guaranteed. The same is true of @Rule, it suffers from the same lack of guarantee. If this will always be the same code, then there isn’t any point in splitting into two methods.

    If you do have two methods, and more importantly, if you wish to use them from multiple places, then you can combine rules using a RuleChain, which was introduced in 4.10. This allows the specific ordering of rules, such as:

    public static class UseRuleChain {
      @Rule
      public TestRule chain= RuleChain
                   .outerRule(new LoggingRule("outer rule"))
                   .around(new LoggingRule("middle rule"))
                   .around(new LoggingRule("inner rule"));
    
      @Test
      public void example() {
          assertTrue(true);
      }
    }
    

    This produces:

    starting outer rule
    starting middle rule
    starting inner rule
    finished inner rule
    finished middle rule
    finished outer rule
    

    So you can either upgrade to 4.10 or just steal the class.

    In your case, you could define two rules, one for client setup and one for object, and combine them in a RuleChain. Using ExternalResource.

    public static class UsesExternalResource {
      private TestRule clientRule = new ExternalResource() {
          @Override
          protected void before() throws Throwable {
            setupClientCode();
          };
    
          @Override
          protected void after() {
            tearDownClientCode()
        };
      };
    
      @Rule public TestRule chain = RuleChain
                       .outerRule(clientRule)
                       .around(objectRule);
    }
    

    So you’ll have the following execution order:

    clientRule.before()
    objectRule.before()
    the test
    objectRule.after()
    clientRule.after()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is simple. For unit test purposes, I need a statically compiled type
In a unit test I need to import a csv file. This is located
I need to use the ReSharper Unit Test Runner to run my MSTest Unit
I need to write a unit test for a method that will print a
I'm writing a unit test for a custom subclass of org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter<T> and I need
I need to know, in a context of a unit test, if Jetbrains IntelliJ
I'm writing an upload function for ColdFusion of Wheels and need to unit test
My unit test keeps getting the following error: System.InvalidOperationException: The Connection is not open.
Im trying to unit test a class in some Java code that I've inherited.
I need to test my vigorously tested code works when called by many threads

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.