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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:03:48+00:00 2026-05-24T17:03:48+00:00

I have a code like this: public class MyTest extends TestCase { private MyObject

  • 0

I have a code like this:

public class MyTest extends TestCase {
    private MyObject mObject1;
    private MyObject mObject2;
    ...
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }
    public void testSomething() {
        mObject1 = new MyObject();
        mObject2 = new MyObject();
    }
    public void testSomething2() {
      // Here I can't access the previously created objects mObject1 and 
      // mObject2, because they are again null. 
      // Why is that, if *my* setUp() method doesn't touch them?
    }

My guess is that JUnit instantiates the class again every time. Can someone please explain me the workflow?

Thanks.

  • 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-24T17:03:49+00:00Added an answer on May 24, 2026 at 5:03 pm

    JUnit will instantiate the class (MyTest) once per test and then execute the methods

    • setUp()
    • testXXX()
    • tearDown()

    until it runs all the methods that start with test and don’t receive any parameters. So in your example, Junit will instantiate MyTest twice. You can read more about this in the JUnit documentation.

    Bear in mind that this is the old way of writing tests. From Junit 4 (I think) the preferred way is to use annotations. You can check the annotations documentation here.

    As a side note, NUnit, reuses the instance of the test, so in the same scenario, it would only instantiate MyTest once.

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

Sidebar

Related Questions

I have code that looks like this: public class A { public void doStuff()
I have code that looks like this: template<class T> class list { public: class
Say, I have a code snippet like this: public static void main(String[] args) {
I have read a parent Class called MyClass code like this, public class MyClass
I have some code like this: public class EffectValues : IEnumerable<object> { public object
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I have some code like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Save([Bind(Prefix=)]Person person) { String s
I have an enum whose code is like this - public enum COSOptionType {
In our application, I have seen code written like this: User.java (User entity) public
We have several areas where code like this can be seen public Map extractData(ResultSet

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.