Reputation: 395
I've seen a lot of examples of how to test if a certain method has been called. But why would one want to do this form of testing? Wouldn't it be more effective to test the outcome of whatever said method is doing?
If I would want to test add(X, Y) just to see if it was called... why would I not just assert that add(X, Y) == X + Y? In that case not only do I verify that the method is being called, but I'm actually verifying what the method is doing in the same test.
Even in the case of testing a method being called X times, say Summation(N) where it would call add N times to get a total of N + N - 1 ... + 0; I still cant see the value in just testing how many times add was called.
Could someone give some helpful advice to a starting Test Engineer to understand this. Perhaps an example of why testing if a method is called is important, and why not just testing its outcome would be more necessary?
Some clarification I guess is necassary. Specifically I'm thinking of the Mockito.verify method. Why would one want just want to verify if a method was called. My understanding of Mockito.Verify is as follows.
public void testSummation(){
Calculator.Summation(X);
//which I guess I would assume called Add() X times.
Verify(Calculator times(x).add();
(note this is just a generic example not actual code I've written) But why would you want to verify that add was called X times? wouldn't testing that the proper value returned from Summation be a better test?
Upvotes: 0
Views: 397
Reputation: 18109
The test object should behave correctly. Not only set it's internal state correctly given some defined input, but also collaborate with other objects through their defined interfaces in a correct manner. Function calls are often part of the defined interface between objects so it is important to test that the test object follows the rules of these interfaces. In other words, calls the correct methods at the right time with the correct parameters and handles the response correctly.
Upvotes: 1
Reputation: 1927
Well, the addition example is fine with just unit-testing as you noticed.
But assume you are building a calculator application, and obviously when a user is requesting an addition operation (your call to add(X,Y)) you would expect that the result is presented to the user.
You would of course need to test the addition operation (assert that add(X, Y) == X + Y), but how would you test that the result was presented to the user? Don't you need a way to verify that the printResult (the name of the method used to print the result) was called?
Upvotes: 0
Reputation: 1587
Take this for example
public Integer AddSum(Int a, Int b){
Integer x= a+b;
// Testing : Log message to confirm x=a+b was completed
String f = x;
//Testing : Log message here does NOT get called as the above statement cause code to crash
return x;
}
A function can have many parts of it, you want to know which line the function reached before crashing/ or checking for unexpected data values. In the example if we just added two numbers, it doesnt really make a difference but as soon as additional stuff is done, its important. For example, any code uses JSONObjects should always be logged/ tested as they often give errors and can be tricky to deal with
Upvotes: 0