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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:58:39+00:00 2026-06-17T03:58:39+00:00

I have a class. I initialize a variable in the constructor of that class.

  • 0

I have a class.
I initialize a variable in the constructor of that class.
I call a method that contains a while loop and increments the variable each time through.
I wrote a test to check the value of the variable after the method has been called (and goes through the while loop one time).

public class ThreadGenerator implements Runnable {
  private int requests;
  private int limit;

  public ThreadGenerator() {
    requests = 0;
  }

  public void setRequestLimit(int anyLimit) {
    this.limit = anyLimit;
  }

  public void generateThread() {
    new Thread(this).start();
  }

  public void run() {
      while(requests < limit) {
          try {
              // do some stuff
              requests++;
              // close some stuff
          } catch (IOException e) {
            e.printStackTrace();
          }
      }
  }

  public int getRequests() {
      return requests; // calling this method from my tests always returns 0
  }

In my test, when I create a new instance of this class, then call this method on that class, it runs correctly and it increments the request counter correctly. I’ve tried several print statements to make sure of that. But if I call getRequests on my ThreadGenerator object in my test, it will return 0, the amount it was initialized with.

My test code:

ThreadGenerator threadGenerator = new ThreadGenerator();
threadGenerator.setRequestLimit(1);
threadGenerator.generateThread();
assertEquals(threadGenerator.getRequests(), 1);

How can I modify this variable I initialized in the constructor and gain access to it in my test suite?

  • 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-06-17T03:58:40+00:00Added an answer on June 17, 2026 at 3:58 am

    Bear in mind that just because you ask Java to create a new thread with a Runnable, doesn’t mean that the run() method will be called immediately. It’s likely the case that the assertEquals is happening before the run happens the first time.

    You may want to return the thread and call join in the test on the generated thread, which will ensure that the Thread runs until it dies, possibly with a short timeout.

    /* in the system under test */
    @VisibleForTesting Thread generateAndReturnThread() {
      Thread thread = new Thread(this);
      thread.start();
      return thread;
    }
    
    public void generateThread() {
      generateAndReturnThread();
    }
    
    /* in the test */
    @Test public void yourTest() {
      ThreadGenerator threadGenerator = new ThreadGenerator();
      threadGenerator.setRequestLimit(1);
    
      // wait up to a second for thread to complete
      threadGenerator.generateThreadAndReturn().join(1000);
    
      assertEquals(threadGenerator.getRequests(), 1);
    }
    

    Side note: Consider AtomicInteger for your requests class, if multiple threads might modify requests. This will help prevent two different threads from both modifying requests and overwriting one another.

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

Sidebar

Related Questions

I have a class where I need the constructor to initialize variable array. I
I have a Rack application that looks like this: class Foo def initialize(app) @app
I have in my header Test.h a variable of a class that doesn't have
I have a Javascript class that contains a few functions and member objects: function
I have a SiteNavigation class that is being updated in the Initialize event of
WorkAround: I have declared a class level Public static variable and initialized with a
Let's say I have a class Foo with some primitive instance variables. I initialize
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have a class: class One def initialize; end end I need to create
I have this class: public MyClass { public void initialize(Collection<String> data) { this.data =

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.