When using Mokito, what is the difference between the actual object and the mocked object?

In the program below, I am trying to use mockito with junit in my test case. But I don’t see how Mokito helps create objects for my test? I don’t see anything special here, as it seems that mokito creates an instance of the object.

public class TestCase1{ @Mock MyClass myClass; public void setup(){ MokitoAnnotations.initMoks(this); } @Test public void testAddition(){ when(myClass.add(2,2)).thenReturn(20); assertEquals(4,myClass.add(2,2)); } } 

My actual class ( MyClass.java )

  public class MyClass{ public int add(int x, int y){ return x+y; } } 

Is it a mockery of an object, as is an injection (DI) of an object? I appreciate your help!

0
java unit-testing
source share
1 answer

Your test does not validate your code. He checks if Mockito is working.

When I introduce the concept of ridicule, I take this example: suppose you are creating a detonator and want to test it. Of course, you can use a detonator with a real bomb and see if the whole block explodes when you use a detonator. But this is not very practical. By the way, maybe you don’t even have a bomb at your disposal. Perhaps your colleague is still building it.

So, you are using a bomb layout. Pay attention to an important point: you use a bomb model to check the detonator. Not a fake detonator. What taunts is the dependency of the tested class. Not the class itself under the test.

What is a bomb model? It is just a fake bomb that does nothing. All that he does is to check whether it was proposed to explode or not. Therefore, your code for checking the detonator will be like this:

 // create a mock bomb: Bomb mockBomb = mock(Bomb.class); // create a real detonator, but tie it to the mock bomb: Detonator detonator = new Detonator(mockBomb); // test the detonator. Since it tied to a mock bomb, the block // won't explode detonator.pressTheRedButton(); // check it the mock bomb has been asked to explode, as it should // if the detonator works correctly verify(mockBomb).explode(); 

Now, if the test passes, you know that all of the internal circuits that are used in pressTheRedButton() work fine and ultimately report a bomb explosion. So you know that when used with a real bomb, a real bomb will also leak out when you press the red button.

Now back to the real world: you want to test the service, and this service uses the DAO, which needs a database filled with data in order to work properly. To test your service, you can simply scoff at the DAO and make sure that it works fine. The DAO layout can also be used as a stub, that is, an object that returns what you say to be returned in the test, instead of actually querying the database. This is what you do in the code in your question: you tell the MyClass layout that when add() is called with arguments 2 and 2 as arguments, it should return 4.

This simplifies the setup of the test, accelerates its launch and does not depend on the actual DAO code, which is not what you want to test in the unit test of the service.

+8
source share

All Articles