How to test local variable in method using junit test case?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CSunil111
    New Member
    • Mar 2017
    • 1

    How to test local variable in method using junit test case?

    Hi friends,

    Being new in junit i have onw question that How to test local variable in method using junit test case?
    Check that below is my method suppose,

    Code:
    public void methodToCheckStr(int testVar)
    	{
    		String str="";
    		if(testVar==5)
    		{
    			str="Success";
    		}
    		else
    		{
    			str="fail";
    		}
    	}
    How to create test case to check what is 'str' contains?

    Kindly help me out here.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You can't.
    If it would be a field instead a local variable, you could test it via reflection.

    But anyway, the method can return "str" as a value and then you can test against that value.

    That means in JUnit you are only testing the result of public methods.

    Comment

    Working...