Switch
Switch

Reputation: 15453

Junit | How to test parameters in a method

How do I test parameters inside a method itself. For example

class TestClass {

public void paraMethod(String para1, String para2) {
         String testPara1 = para1;
         String testPara2 = para2; 
}
}

class TestingClass {

   @Test
   public void testParaMethod () throws Exception {
             String myPara1 = "MyPara1"; 
             String myPara2 = "MyPara2";

             new TestClass().paraMethod(myPara1, myPara2);

   }
}

Ok, so is it possible to test if the testPara1 and testPara2 are properly set to the values that I have passed?

Thanks.

Upvotes: 2

Views: 3310

Answers (2)

Romski
Romski

Reputation: 1942

If I understand you correctly, you want to test that the variables testPara1 and testPara2 hold the values supplied to para1 and para2 respectively. Given that the method does nothing, it shouldn't really matter.

To improve your unit test pick a name that describes what the method does. Decide what the out come is (a value is returned, some state is changed) and test for the expected outcome based on known input. Write your test without implementing the method, thinking about valid/invalid input and possible edge cases. Then implement you method until all tests pass - when they do move on.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726559

It is neither possible nor desirable to test the values and state changes of local variables of a method. They are private details of the implementation, not something you are expected to test.

Instead, your tests should judge the correctness of a method by observing something visible from the outside, such as return values, interactions with mocked environment, or externally visible changes of the object's state.

Upvotes: 8

Related Questions