[JAVA] What I learned ② ~ Mock ~

Introduction

I'm using Mock for unit tests, so I'd like to summarize it easily as a review.

What is Mock

Simply put ** "Called from the class under test, A substitute for parts (other sources). "

For example, a test target class that outputs calculation results, Suppose you have a subclass that actually does the calculations for you.

Mock takes the place of this subclass. That is.

Mock Summary

1. Mock object generation

Sub sub = Mock(Sub.class);

Create a mock object using the Mock method

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

//a is the part call processing
//b is the value returned by the called part
Mockoito.when(sub.getAns()).thenReturn(b);

I'm calling the Sub class in the source under test, When passing the test, if the part called Sub is not completed The test fails at the call part and I can't pass it to the end. Therefore, when the getAns method of Sub class is called using the above process, If you instruct it to return the value b, you can pass the process in the test.

3.MockObjectManager.setReturnValueAt

//1st argument=Class designation
//2nd argument=Method name
//3rd 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 the number of calls to the third argument. If the same part is used multiple times in the source under test, It is necessary to write according to the number of times.

4.Mockito.any

//Mockito.any example
//↓ A setting that returns b no matter what int type comes
Mockito.when(sub.getAns(Mocito.anyInt(), Mockito.anyInt())).thenReturn(b);

In the Main class to be tested, without specifying the argument when calling sub.getAns If it is an int type, it is good.

This is mainly used when you want to see the coverage mainly. There are also 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();

//↓ ↓ Other things I was using
//Specify the variable sub declared in the field of Main class
Field field = Main.getDeclaredField("sub");

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

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

at the end

If you use it in a series of steps for Mocking, it will be as follows.

Example 1


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

//If getAns is a method of adding two arguments
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);

//When the formal argument of getAns is DTO type
AnsDTO ansDto = new AnsDTO();
ansDto.setMath1(1);
ansDto.setMath2(1);

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

Recommended Posts

What I learned ② ~ Mock ~
What i learned
What I learned ① ~ DJUnit ~
What I learned about Kotlin
What I learned from studying Rails
What I learned with Java Gold
What I learned with Java Silver
What I learned through the swift teacher
What I learned from Java monetary calculation
Summary of what I learned about Spring Boot
What I learned in Java (Part 2) What are variables?
Summary of what I learned in Spring Batch
I will write what I learned about docker anyway (second)
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
[Rilas] What I learned in implementing the pagination function.
I will write what I learned about docker anyway (first)
I knew what reflection was
What I researched about Java 6
What you learned about hashes
What I researched about Java 9
What you learned about symbols
What I researched about Java 7
What I researched about Java 5
What I learned in Java (Part 4) Conditional branching and repetition
Beginner Ruby on Rails What I learned is being summarized
What I have learned in Java (Part 1) Java development flow and overview
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned from doing Java work with Visual Studio Code
What I researched about Java learning
[Rails] What I learned from a little stumbling block when using ancestry
What I investigated in Wagby development Note 1
Take what you've learned about Java reflection
What Rails beginners learned by solving errors
Twitter marketing? What if I automate it?
What I got into @Transactional in Spring
What is Docker? I tried to summarize
What I stumbled upon when installing Laravel-filemanager