[Java beginner's anguish] Hard-to-test code implemented in Junit

One day I was writing this code

@Inject
private Accessor accessor;

@Dependent
public class DataReader {

   String getData(String key) {

		String value = accessor.getByDate(key, new Date());

		return value;

	}
}	

Let's test! Here is the test I wrote thinking

public class DataReaderTest {
	
	@Mock
	Accessor accessor;
	
	@InjectMocks @Spy
	DataReader dataReader;
	
	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void test() {
		when(accessor.getByDate(key, new Date())).thenReturn(now);
		assertThat(dataReader.getData("key0"), is("todayData"));
	}
}
However, when I consulted with him that he wouldn't succeed ... he said, "This won't succeed, it's hard to test."
In Test when (accessor.getByDate (key, new Date ())). thenReturn (now); is actually called ʻaccessor.getByDate (key, getNow ())`, the value of the Date type argument changes. I failed because I ended up

Revised

@Inject
private Accessor accessor;

@Dependent
public class DataReader {

   String getData(String key) {

		String value = accessor.getByDate(key, getNow());

		return value;

	}
	
	Date getNow() {
		return new Date();
	}
}
public class DataReaderReaderTest {
	
	@Mock
	Accessor accessor;
	
	@InjectMocks @Spy
	DataReader dataReader;
	
	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void test() {
		Date now = new Date();

		//This is also good
		//when(deviceOptionReader.getNow()).thenReturn(now);
		doReturn(now).when(deviceOptionReader).getNow();

		when(accessor.getByDate(key, now)).thenReturn(now);
		assertThat(dataReader.getData("key0"), is("todayData"));
	}
}

Commandments

Do not write code that creates a new Date type directly in the method argument

Recommended Posts

[Java beginner's anguish] Hard-to-test code implemented in Junit
Java in Visual Studio Code
Write Java8-like code in Java8
Guess the character code in Java
Rock-paper-scissors game for beginners in Java
Java Spring environment in vs Code
[For beginners] Run Selenium in Java
All same hash code string in Java
[Mac] Install Java in Visual Studio Code
Implemented basic search / sort algorithm in Java
Add --enable-preview option in Java in Visual Studio Code
Technology for reading Java source code in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
Static code analysis with Checkstyle in Java + Gradle
Code to escape a JSON string in Java
Try using Sourcetrail (win version) in Java code
Try using Sourcetrail (macOS version) in Java code
[AtCoder Problem-ABC001] C-Do wind observation in Java [Code]
[Mac] Install Java in Visual Studio Code (VS Code)
Script Java code
Java code TIPS
Partization in Java
Java sample code 02
Java sample code 03
Rock-paper-scissors in Java
Java sample code 04
Java character code
Pi in Java
FizzBuzz in Java
Sample code to convert List to List <String> in Java Stream
Java beginners make poker games in 4 days (3rd day)
Do not write if (isAdmin == true) code in Java
[For beginners] Minimum sample to display RecyclerView in Java
Java beginners make poker games in 4 days (2nd day)
Java11: Run Java code in a single file as is
Differences in code when using the length system in Java