Shyna
Shyna

Reputation: 145

How to assert that methods are called with mockito

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?

Upvotes: 4

Views: 7479

Answers (2)

djechlin
djechlin

Reputation: 60748

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.

Upvotes: 2

zubergu
zubergu

Reputation: 3706

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

Upvotes: 7

Related Questions