-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventControllerTest.java
More file actions
45 lines (36 loc) · 1.47 KB
/
Copy pathEventControllerTest.java
File metadata and controls
45 lines (36 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.techevents.controller;
import com.techevents.model.Event;
import com.techevents.repository.EventRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(EventController.class)
public class EventControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private EventRepository eventRepository;
@Test
public void testGetAllEvents() throws Exception {
Event event = new Event(
"Test Event",
"Sample Description",
LocalDate.of(2025, 6, 1),
"New York",
List.of("tech", "conference")
);
when(eventRepository.findAll()).thenReturn(List.of(event));
mockMvc.perform(get("/events"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].title").value("Test Event"))
.andExpect(jsonPath("$[0].city").value("New York"))
.andExpect(jsonPath("$[0].tags[0]").value("tech"));
}
}