Sometimes I would like to test an intermediate value in a method. But the method can’t be split. So I wonder if JUnit can only test a method as a unit. If I can put something like a breakpoint in a method, and get the local value and assert it, it will be better. If it is impossible, how do you solve this kind of problem.
Share
Adding a break point is not really unit testing and in fact not testing at all. You can call it checking but there is no such thing as Check Driven Development.
Anyhow, people new to Unit testing quite often find this issue that they want to test private or local values let that be a variable or method. When I started TDD I was in fact quite phased by it myself. But the thing is, in Unit testing the purpose is to do black box testing of a particular functionality and all possible outcomes from that. What happens inside that black box is not your concern nor do you need to unit test it.
If you feel like testing an intermediate value then maybe your method is too long and should be split into two or more testable small methods. And if it can not be done then you should not be worrying about testing them.
If that intermediate value is getting its value from another public method then it’s that method that you want to test in separate unit test, not the value inside the method you are currently testing.