JUNIT testing void methods

后端 未结 7 1082
遇见更好的自我
遇见更好的自我 2020-11-27 02:41

I have a java class full of void methods, and I want to make some unit test to get maximal code coverage.

For example I have this method :

protected          


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 03:23

    You can still unit test a void method by asserting that it had the appropriate side effect. In your method1 example, your unit test might look something like:

    public void checkIfValidElementsWithDollarSign() {
        checkIfValidElement("$",19);
        assert ErrorFile.errorMessages.contains("There is a dollar sign in the specified parameter");
    }
    

提交回复
热议问题