5

I want to ask more of a conceptual question related to testing. I am using Mockitos for my unit testing.

I have a method that does bunch of things. All the methods it calls internally are void methods, which in turn will do some other actions. The method under question can complete with exception or without exception. There is no scope for writing assert statements as it mostly calls void methods. Also, I have written independent unit test for all other methods of this class.

My question is if I need test coverage, I should write test case for all methods. But, I do not find any valid assert for one of my method. The method under question is an important method from unit test coverage perspective. When I run my test, I do see in logs that the methods executes fine but there is really nothing to assert. What should I do in such a situation?

1) Leave the method as is without any asserts? It still is checking if everything is working as expected

2) Create 2 methods one that expects exception ( Negative Testcase) and one regular test method with no asserts. ( This is to basically show I have covered both positive and negetive scenario). I still do not have any asserts here.

Is this the right approach to take or there is a better way for dealing with this problem?

asked Oct 12, 2015 at 22:28
2
  • 1
    You say you are using Mockito for your test. Are you using mocks of the objects being called by this method under test? in that case, you can use Mockito's verify() to verify that the appropriate other methods were called as expected. You are then verifying behavior, and don't need asserts. Commented Oct 12, 2015 at 22:32
  • @nikpon Please read the full problem statement, I did say that I have covered all the methods with assert that are called from this method. Commented Oct 12, 2015 at 22:37

2 Answers 2

8

If you don't have anything worth asserting, you can try verification instead.

verify(someMock, times(x)).someMethod();

More on verification with Mockito in the documentation: https://mockito.googlecode.com/hg-history/1.5/javadoc/org/mockito/Mockito.html

answered Oct 12, 2015 at 22:35
Sign up to request clarification or add additional context in comments.

1 Comment

+1 because this is probably what the user wants, but I definitely stand by my comment about figuring why the heck the OP doesn't have anything to assert.
2

Why isn't there anything to assert? Let A be the universe if you run your method. Let B be the universe if you don't run your method. If A == B then stop calling the method, it's not doing anything. If A != B then assert that whatever is different about them is true.

i.e., what is your method actually doing? That should be part of your test.

answered Oct 12, 2015 at 22:37

3 Comments

This method is like a execute method which calls all different methods to complete execution. That is the reason it is not doing anything explicitly. It is just like a holder method. if any of the methods that are called are failing with some exception, this method catches it. That is why I can create a negative testcase by passing value that can allow other method to fail thereby allowing it to throw exception. For positive testcase I was not sure if I can do something other than calling verify.
@Shyna yes, Mockito can do that. I'd first request that you explicitly change your question to something like, "how to assert that methods are called with mockito." Using mocks + verify is the right thing to do. I can put together a full answer (honestly this is probably a duplicate question) after you update your question if you're still stuck.
@djechiln I will update it, I just wanted to check if there was another approach. I am not stuck , Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.