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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:27:48+00:00 2026-06-09T03:27:48+00:00

This is the first time am trying to use reflection for unit testing and

  • 0

This is the first time am trying to use reflection for unit testing and I had this doubt.

Class Example {
  public static Map<Something, Something> someMethod()
  {
    int temp = -1;
    //Some implementation which might change the value of temp
    //depending on other cases
    if(temp == -1)
      //Do something and return something
    else
      //return null
  }
}

Now in the above snippet, I can get the initial value of the variable temp using reflection. I wanted to know, if the value of the variable changes while execution of the code how can I get the new value of temp? Am a total newbie to reflection so if this sounds silly please tolerate.

P.S The actual code am testing is not such a simple one. I have a feeling that I cannot unit test the last if condition without using reflection or powermock.

  • 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-09T03:27:50+00:00Added an answer on June 9, 2026 at 3:27 am

    You cannot use reflection to access the state of a local variable. Instance variables yes, static variables yes, local variables no.

    Also, as @platzhirsch notes, reflection does not allow you to attach triggers to variables of any kind. That kind of thing requires bytecode modification of some kind. However, unit testing doesn’t require triggers. Examination of values before and after should be sufficient.

    I think your real problem here is that your classes have not been designed to make unit testing easy. Firstly you’ve got a static method, and they are hard to “mock”. Secondly, it appears that the logic you are trying to test is embedded inside a method. It would be much easier if (for example) temp was an instance variable, or the code in the // Some implementation ... was a method that you could mock.


    By the way, your use of the term “private” is misleading in this context. Most Java programmers would interpret “private” as meaning a static or instance variable with the private modifier. But your example has temp declared as a local variable, for and the private modifier is not allowed for local variables.

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

Sidebar

Related Questions

this is my first time trying to use document classes in AS3 and im
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
This is my first time trying to use the XMLRPC::Client library to interact with
This is my first time trying to use any SCM so please bear with
This is my first time trying to use both ARC and Core Data. I
This is my first time really trying to use jQuery for my personal portfolio
this is the first time I'm trying to use html5 to display a video.
this is my first time trying to use java for backend web systems. I
This is my first time trying to use Web Start, and I am trying
This is my first time trying Pandas. I think I have a reasonable use

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.