I am new to Spring, and also Junit testing. I would like to test this method so it increases my test line coverage.
Here is the class:
@RestController
@RequestMapping("/")
public class SummaryController {
private final SummaryService summaryService;
@Autowired
public SummaryController(SummaryService summaryService) {
this.summaryService = summaryService;
}
@GetMapping("/summary")
@ResponseBody
public List<SummaryObject> getDirectoryList() {
return summaryService.getSummary();
}
}
So this is a springboot/react web. application. When we click the "summary" page, we load a table with my local file directory content. summaryService.getSummary() is the method that returns a populated object model which my controller then sends the same object back to the front-end.
Here is a test I wrote to try to test this method.
@WebMvcTest (TestController.class)
public class Test Controller {
List<SummaryObject> summaryObject;
@Mock
private SummaryService summaryService;
@Test
void testControllerMatch() {
this.summaryObject = new ArrayList<>();
this.summaryObject.add(new SummaryObjectModel("file name", "file size", "last modified"));
when(SummaryService.getSummary()).thenReturn(summaryObjectModel);
assertEquals(SummaryObjectModel, SummaryService.getSummary());
}
}
The test is passing, but I do not think it is testing what I want. I wanted it to test that the return statement returns the List of SummaryObjects.
I know the return statement is not tested because when I see coverage, the "return summaryService.getSummary();" line in my controller doesn't get tested (its red, not green. thus not tested).
How can I test that method indeed returns a List of SummaryObjects ?
What other kinds of tests can I write for such a simple method? How do I make that line covered in coverage ("green")?