[JAVA] How to mock some methods of the class under test

Recently I'm wondering whether to write in note or Qiita.

How to write when you always forget and always want to mock a part.

--Method you want to mock

public class HighHolidayService {

	//Holiday list organized on a monthly basis
	Map<String, List<LocalDate>> highHolidayCache = new HashMap<>();
    //Method to be tested
	public List<LocalDate> getHolidayList(LocalDate targetDate) {
		String key = String.format("%d%02d", targetDate.getYear(), targetDate.getMonthValue());
		if (highHolidayCache.containsKey(key)) {
			return highHolidayCache.get(key);
		List<LocalDate> holidayList = selectRecords(targetDate);
		highHolidayCache.put(key, holidayList);
		return holidayList;

    //I want to mock this method!!!!
	List<LocalDate> selectRecords(LocalDate targetDate) {
            return null;

--How to write a test

package jp.co.hogehoge.service;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doReturn;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.mock.mockito.SpyBean;

public class HighHolidayServiceTest {

	@SpyBean    //of springboot@How to write spy
	HighHolidayService highHolidayService;
    public void init() {
public void Get the target holiday list() {
		List<LocalDate> returnHolidayList = new ArrayList<>();
		returnHolidayList.add(LocalDate.of(2019, 07, 28));
		returnHolidayList.add(LocalDate.of(2019, 07, 21));
		returnHolidayList.add(LocalDate.of(2019, 07, 15));
		returnHolidayList.add(LocalDate.of(2019, 07, 14));
		returnHolidayList.add(LocalDate.of(2019, 07, 7));
        // doReturn(Object to return).when(spy object).method()
        //Forget this way of writing! !!

		LocalDate date = LocalDate.of(2019, 7, 15);
		List<LocalDate> holidayList = highHolidayService.getHolidayList(date);

It's okay to mock each class because I often do it, but I forget to mock a part of the test target because it only occasionally appears. .. Or rather, there are a lot of talks that it is better to classify (yes, but it's logic that doesn't need to be that much).

Recommended Posts

How to mock some methods of the class under test
Part of the class called from the class under test in JMockit Mock method memo
How to use the wrapper class
Various methods of the String class
How to use class methods [Java]
[Java] How to use the File class
[Java] How to use the HashMap class
How to determine the number of parallels
[Processing × Java] How to use the class
How to sort the List of SelectItem
[Java] How to use the Calendar class
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
How to find the cause of the Ruby error
How to execute and mock methods using JUnit
Customize how to divide the contents of Recyclerview
How to get today's day of the week
Output of how to use the slice method
How to display the result of form input
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
How to write Scala from the perspective of Java
Get the name of the test case in the JUnit test class
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
[Rails] How to change the column name of the table
[SwiftUI] How to specify the abbreviated position of Text
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
How to judge the click of any area of the image
How to download the old version of Apache Tomcat
[Swift] How to get the document ID of Firebase
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to get the class name / method name running in Java
How to display the select field of time_select every 30 minutes
How to use java class
How to validate subsequent tests even if the test fails
How to get the longest information from Twitter as of 12/12/2016
How to move another class with a button action of another class.
[Ruby] How to retrieve the contents of a double hash
How to add elements without specifying the length of the array
Let's summarize how to extend the expiration date of Rails
[Rails] How to display the list of posts by category
How to derive the last day of the month in Java
I want to recursively search the class list under the package
How to change the contents of the jar file without decompressing
How to check the extension and size of uploaded files
[jsoup] How to get the full amount of a document
How to check the database of apps deployed on Heroku
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Rails] How to get the URL of the transition source and redirect
[Swift5] How to get an array and the complement of arrays
How to set the IP address and host name of CentOS8
How to display 0 on the left side of the standard input value
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Swift UI] How to get the startup status of the application [iOS]
How to get the contents of Map using for statement Memorandum
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
How to get the id of PRIMAY KEY auto_incremented in MyBatis