[Java] What I learned ② ~ Mock ~

2 minute read

Introduction

I’m using Mock for unit testing, so I’d like to easily summarize it while also looking back.

What is #Mock Briefly, ** “Called from the class under test, It will be a substitute for parts (other sources).”

For example, the test target class that outputs the calculation result, Suppose there is a subclass that actually does the calculations.

Mock is a substitute for this subclass. That means.

Mock summary

1. Mock object generation

Sub sub = Mock(Sub.class);

Generate mock object using Mock method

2.Mockito.when().thenReturn();

//a is the calling process of the component
//b is the value returned by the called component
Mockoito.when(sub.getAns()).thenReturn(b);

I’m calling the Sub class in my test source, When passing the test, if the part called Sub is not completed, The test failed at the calling part and I can not pass it to the end. Therefore, using the above process, when the getAns method of Sub class is called, If you tell it to return a value of b, you can let the test work.

3.MockObjectManager.setReturnValueAt

// 1st argument = class specification
// second argument = method name
// third argument = number of calls
// 4th argument = return value setting
MockObjectManager.setReturnValueAt(Sub.class, "getAns", 0, b);
MockObjectManager.setReturnValueAt(Sub.class, "getAns", 1, b);

Also used with Mockito.when in 2. Note that the number of times the third argument is called. If the same source is used multiple times in the test source, It is necessary to write according to the number of times.

4.Mockito.any

//Mockito.any example
//↓ b will be returned no matter what the int type is
Mockito.when(sub.getAns(Mocito.anyInt(), Mockito.anyInt())).thenReturn(b);

In the Main class of the test target, without specifying the argument when calling sub.getAns, Int type is good.

This is mainly used when you want to see coverage mainly. Other than this, there are Mockito.anyString(), Mockito.anyMap(), and cast (class) Mockito.any.

Others used in tests using Mock


//Main=source to be tested
Main main = new Main();
//Sub=Other class parts
Sub sub = new Sub();

//↓↓ Others used
//Specify the variable sub declared in the field of the Main class
Field field = Main.getDeclaredField("sub");

//Remove access restrictions to private variables
field.setAccessible(true);

//Set value to private variable
field.set(main, sub);

at the end

Mocking is as follows when used in a series of flows.

Example 1


Sub sub = Mock(Sub.class);
Field field = main.getDeclaredField("sub");
field.setAccessible(true);
field.set(main, sub);

//if getAns is a method with two arguments added
Mockito.when(sub.getAns(1,1)).thenReturn(2);

Example 2


Sub sub = Mock(Sub.class);
Field field = main.getDeclaredField("sub");
field.setAccessible(true);
field.set(main, sub);

//If the parameter of getAns is DTO type
AnsDTO ansDto = new AnsDTO();
ansDto.setMath1(1);
ansDto.setMath2(1);

Mockito.when(sub.getAns(ansDto)).thenReturn(3);