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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:33:31+00:00 2026-05-16T16:33:31+00:00

I believe that we are all know that setUp (@Before) will execute before any

  • 0

I believe that we are all know that setUp (@Before) will execute before any test method and tearDown(@After) will execute after test method.

Also we know that Junit will create one instance of Test per test method.

my question is that can we just move setUp method content to class Constructor and remove setUp method? is there any specific reason to keep setUp method?

  • 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-16T16:33:31+00:00Added an answer on May 16, 2026 at 4:33 pm

    This (old) JUnit best practices article puts it like this:

    Do not use the test-case constructor to set up a test case

    Setting up a test case in the
    constructor is not a good idea.
    Consider:

    public class SomeTest extends TestCase
       public SomeTest (String testName) {
          super (testName);
          // Perform test set-up
       }
    }
    

    Imagine that while performing the
    setup, the setup code throws an
    IllegalStateException. In response,
    JUnit would throw an
    AssertionFailedError, indicating that
    the test case could not be
    instantiated. Here is an example of
    the resulting stack trace:

    junit.framework.AssertionFailedError: Cannot instantiate test case: test1   
        at junit.framework.Assert.fail(Assert.java:143)
        at junit.framework.TestSuite.runTest(TestSuite.java:178)
        at junit.framework.TestCase.runBare(TestCase.java:129)
        at junit.framework.TestResult.protect(TestResult.java:100)
        at junit.framework.TestResult.runProtected(TestResult.java:117)
        at junit.framework.TestResult.run(TestResult.java:103)
        at junit.framework.TestCase.run(TestCase.java:120)
        at junit.framework.TestSuite.run(TestSuite.java, Compiled Code)
        at junit.ui.TestRunner2.run(TestRunner.java:429)
    

    This stack trace proves rather
    uninformative; it only indicates that
    the test case could not be
    instantiated. It doesn’t detail the
    original error’s location or place of
    origin. This lack of information makes
    it hard to deduce the exception’s
    underlying cause.

    Instead of setting up the data in the
    constructor, perform test setup by
    overriding setUp(). Any exception
    thrown within setUp() is reported
    correctly. Compare this stack trace
    with the previous example:

    java.lang.IllegalStateException: Oops
        at bp.DTC.setUp(DTC.java:34) 
        at junit.framework.TestCase.runBare(TestCase.java:127)
        at junit.framework.TestResult.protect(TestResult.java:100)
        at junit.framework.TestResult.runProtected(TestResult.java:117)
        at junit.framework.TestResult.run(TestResult.java:103)
        ...
    

    This stack trace is much more
    informative; it shows which exception
    was thrown (IllegalStateException) and
    from where. That makes it far easier
    to explain the test setup’s failure.

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

Sidebar

Related Questions

All, I'm attempting to write a Junit test that calls a Web Service client
I used to believe that any variable that is shared between two threads, can
I don't believe that I am using any 3.0 specific APIs, but somehow whenever
I am architecting a solution that will use WCF to make all the computers
I would like to know if there is a way to execute some method
I have another question for you that I believe I already know WHY it
I believe that the rallytagpicker is definitely a useful component. However, I do not
I believe that UIView objects can delegate control to views in iOS programming. However,
I believe that I did everything necessary to change my app for ipad (was
I believe that in C99, modification of string literals is undefined behaviour. I don't

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.