Unit testing and approval for the void method

I am trying to create some unit testing for the void method. In principle, this method is designed to show the role of the user of the system and implement it in software.

This is the method:

public void setPersonObj(Person typeObj) { this.typeObj = typeObj; createMain(); } 

How can I create an assert argument in a separate class that uses unit testing to test this method?

Many thanks

+7
source share
3 answers

If the method is void , it clearly has some side effects. Otherwise, it would be non-op. Thus, you have no choice and need to check for these side effects.

How to check for these side effects depends on the technology used and the testing approach:

  • if the method calls some other collaborators / objects, mock and then checks mocks

  • if it changes the state of some other components (adding elements to the collection, changing the field) and approves them

  • if it stores something on disk / database, request them also

  • if it displays some window in Swing, you need to use Swing testing framework like Window Licker

  • if ... could you provide some technical details ?

BTW A typesetter who runs some additional unrelated logic is code smell. It will be very difficult for people who support such code to understand that this innocent setPersonObj() doing something other than ... customization.

Also, all of these names: Person , PersonObj and typeObj must be the same for consistency and compatibility with the JavaBean specification.

+13
source

Unit testing has two different philosophies.

  • Condition testing tested class. Usually you run the method you want to test, and make sure that the class under the test has the correct state after running it, using the getters on it or the value returned by the method itself. In this case, it can be a little difficult to test the void method, as you may be forced to add getters only for your test, and this will break the encapsulation. So don’t do it. An alternative way is to use state-captureing stub classes as your dependencies, but this may be suspicious, as there is some unchecked logic in your stub.

  • Behavior testing tested class. When you run the method that you want to test, you set the dependency expectations of your class. This is usually achieved using mock frameworks . In this case, you basically don't care if your method returns a value or is empty. The important thing is what methods are called in your dependencies and with what parameters. This is what you want to do, a much more effective way of testing (and usually provides better coverage). Using this testing method consistently also ensures that your design is good and proper OO (forces you to do dependency injection, etc.)

+1
source

It depends on the createMain() method. For example, if a method sets the value of a String field in a class, your unit test should check to make sure the String value is set to the correct value.

Your unit test can also verify that the typeObj field is typeObj (by calling its corresponding getter method).

0
source

All Articles