I'm working on a new service, for the past year I haven't needed to write tests from scratch. I've either been bug fixing, or adding features that require modification of tests, but not writing everything from scratch.
I need to make sure I'm doing things right. The unit tests below test a service that queries a database for data.
The *Proc files use Springs @Repository annotation, and each one calls a different stored procedure in the database. For these tests I'm mocking everything. Have I done it correctly? Is there anything I have overlooked? Anything I've possibly missed? The tests all pass when I run them in my IDE or with maven. In this particular instance those method calls simply return the object(s) retrieved from the database, so there isn't any actual business logic in there apart from calling the get() method from the proc objects that I'm mocking.
Have I done things the correct way?
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public class FusServiceImplTest {
@Mock
private GetFileInfoByIdProc getFileInfobyIdProc;
@Mock
private GetFileInfoProc getFileInfoProc;
@Mock
private GetFileInfoByFileNameProc getFileInfoByFileNameProc;
@Mock
FileInfoVO voMock;
@Mock
FileInfoVO voMock2;
@InjectMocks
private FusServiceImpl service;
@Test
public void whenGetFileById_thenReturnFile(){
final Long ID = 99999L;
when(getFileInfobyIdProc.get(ID)).thenReturn(voMock);
FileInfoVO voResult = service.getFile(ID);
assertThat(voResult.getFileName())
.isEqualTo(voMock.getFileName());
}
@Test
public void whenGetFileByName_thenReturnFile(){
final String NAME = "test.file";
when(getFileInfoByFileNameProc.get(NAME)).thenReturn(voMock);
FileInfoVO voResult = service.getFile(NAME);
assertThat(voResult.getFileName())
.isEqualTo(voMock.getFileName());
}
@Test
public void whenGetAllFiles_thenReturnAllFiles(){
List<FileInfoVO> list = new ArrayList<>();
list.add(voMock);
list.add(voMock2);
when(getFileInfoProc.get()).thenReturn(list);
List<FileInfoVO> voListResult = service.getAllFiles();
assertThat(voListResult.size())
.isEqualTo(list.size());
}
}