EasyMock: Void Methods

后端 未结 5 718
北荒
北荒 2020-12-04 08:58

I have a method that returns void in a class that is a dependency of the class I want to test.

This class is huge and I\'m only using this single method from it. I n

5条回答
  •  情深已故
    2020-12-04 09:44

    In situations like these I've found that making a nested class in my unit test class and overriding the methods with special requirements in that way is the best route. So if you're testing ClassA which has that method with the parameters you need to access, you'd do something like:

    class MockClassA extends ClassA {
        @Override
        void specialMethod(String param1, String param2) {
            // do logging or manipulation of some sort
            super.specialMethod(param1,param2); // if you need to
        }
    }
    

    In my unit testing code, I then just use this instance instead. Just treat it as if it was any other mock object. Much easier than mixing libraries, which I agree is probably not a good idea.

提交回复
热议问题