I'am trying to test my get all events method. So i'm trying to first fill one event into database and then perform get method by MockMVC. But it is returning empty content.
I have tried to change specific annotations such like MockBean/Autowired on eventService. Also tried to change annotations on test class. Event controller has these 3 annotations:
@RequestMapping("/event")
@CrossOrigin
@RestController
Method to add event
public void addEvent(final Event event)
{
eventRepository.save(event);
}
Method to get events
public Iterable<Event> getAll()
{
return eventRepository.findAll();
}
Test for get method
@SpringBootTest
@AutoConfigureMockMvc
class EventControllerTest extends Specification {
@MockBean
private EventService eventService
@Autowired
private MockMvc mockMvc
def "whet get is performed on all endpoint the response has status 200"()
{
given:
eventService.addEvent(new Event(new Date(), "type", "league", "team", "msg"))
expect: "the status is 200"
when:
ResultActions resultActions = mockMvc.perform(get("/event/all"))
I expect to get method return one event but actual it returns 0.