0

I see similar questions, but not answered properly as I am looking for.

I have void method, which does some changes to the Model object as per the some other DB Values then save it again back to DB.

Pseudo code:

User{
List<Mark> marks;
String fullName;
int grade;
date dob;
}

The logic is to update User grade by applying some formulas by calling another service.

This is how I designed to test it:

I can give the User object with marks as input. I will mock the Service by providing the formula to determine the grades as per the marks. I want to test the final User Object is matching with what I am expecting just before saving.

I know that I can move that to separate method and return the object so that can be unit tested. What I am looking here is if there any way to test the logic inside a void method to verify the updated object. I have several methods like that in our project.

I know there is Mockito.verify or I can call like doNothing etc. This is just to call method and see what is happening inside. But I am not getting no data verification is possible by calling those methods.

It could be great if any one can suggest a solution for this.

I gave an example what the code is available for me. This is generic question though. My question is here how can I test a void method? My question is not how to mock a void method. Since void method does not return anything, but it does some logic and finally it updates DB. I want to test whether the implemented logic is working for different scenarios. The situation is cannot change the original method-writing a unit testing for those methods. Any suggestions?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Shamseer
  • 682
  • 1
  • 11
  • 24
  • 2
    "_I know that I can move that to separate method and return the object so that can be unit tested._" <-- Do this. – jaco0646 Dec 30 '16 at 15:18
  • Thanks @jaco0646 for your quick response. But you mean there is no way to verify the data inside a void method in java unit-testing? – Shamseer Dec 30 '16 at 15:19
  • 3
    I'm not saying you _can't_. I'm saying you _shouldn't_. Don't hack around the design problem with complicated testing. Fix the design. – jaco0646 Dec 30 '16 at 15:23
  • You should provide an example. It's hard to tell what you're describing. I will say this: _**"providing the formula ... I want to test the final User Object is matching with what I am expecting just before saving."**_ sounds like bad test design. Your test shouldn't care about the implementation of the web service, just that it gets invoked. – Christopher Schneider Dec 30 '16 at 15:48
  • I read your posting 3 times; and I have no clue. What is the point of giving us explanations about what you think your code is doing; to then ask how to fix that code?! Come up with a [mcve]; and then we can help. – GhostCat Dec 30 '16 at 19:15
  • Possible duplicate of [How to make mock to void methods with mockito](http://stackoverflow.com/questions/2276271/how-to-make-mock-to-void-methods-with-mockito) – Timothy Truckle Jan 01 '17 at 12:11
  • Tnx guys, I edited my question, is there any way to test a void method? Or any method with void return type cannot be tested? As per my understanding mocking and testing are two things. I want to do unit testing of a void method. What is the best way? – Shamseer Jan 03 '17 at 14:37

1 Answers1

1

The fact your method doesn't return anything is irrelevant. You test a void method same as any other method: setup your inputs, invoke the method, perform required assertions.

If I understand correctly, the issue you are experiencing is that the method does not return anything but as you are passing in the initial User object you already have a reference to the object so you can still perform whatever assertions you need to AFTER the void method completes.

haggisandchips
  • 523
  • 2
  • 11
  • Tnx, this resolves my problem now. I will need to read more about making better unit testable code. Do you have good reference to see more highlight on designing applications to make more unit testable... – Shamseer Jan 03 '17 at 15:07