Unit Testing without Assertions

后端 未结 14 2305
夕颜
夕颜 2020-12-17 08:35

Occasionally I come accross a unit test that doesn\'t Assert anything. The particular example I came across this morning was testing that a log file got written to when a co

相关标签:
14条回答
  • 2020-12-17 09:07

    In general, I see this occuring in integration testing, just the fact that something succeeded to completion is good enough. In this case Im cool with that.

    I guess if I saw it over and over again in unit tests I would be curious as to how useful the tests really were.

    EDIT: In the example given by the OP, there is some testable outcome (logfile result), so assuming that if no error was thrown that it worked is lazy.

    0 讨论(0)
  • 2020-12-17 09:08

    We do this all the time. We mock our dependencies using JMock, so I guess in a sense the JMock framework is doing the assertion for us... but it goes something like this. We have a controller that we want to test:

    Class Controller {
      private Validator validator;
    
      public void control(){
        validator.validate;
      }
    
      public setValidator(Validator validator){ this.validator = validator; }
    }
    

    Now, when we test Controller we dont' want to test Validator because it has it's own tests. so we have a test with JMock just to make sure we call validate:

    public void testControlShouldCallValidate(){
      mockValidator.expects(once()).method("validate");
      controller.control;
    }
    

    And that's all, there is no "assertion" to see but when you call control and the "validate" method is not called then the JMock framework throws you an exception (something like "expected method not invoked" or something).

    We have those all over the place. It's a little backwards since you basically setup your assertion THEN make the call to the tested method.

    0 讨论(0)
提交回复
热议问题