generated from keploy/template
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathPotionControllerTests.java
More file actions
189 lines (154 loc) · 8.43 KB
/
PotionControllerTests.java
File metadata and controls
189 lines (154 loc) · 8.43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.example.potionsapi.controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.potionsapi.service.PotionService;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import com.example.potionsapi.model.Potion;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.hamcrest.Matchers.hasSize;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.MediaType;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.assertj.core.api.Assertions.assertThat;
import org.mockito.ArgumentCaptor;
@WebMvcTest(PotionController.class)
public class PotionControllerTests {
// Add test methods here
// Test generated using Keploy
@Autowired
private MockMvc mockMvc;
@MockBean
private PotionService potionService;
@Test
void shouldReturnHelloWorldHtml_whenHomePageEndpointCalled_Fix1() throws Exception {
// Arrange (No specific arrangement needed for this simple endpoint)
// Act & Assert
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().string("<h1> Hello world </h1>"));
}
// Test generated using Keploy
private com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
@Test
void shouldReturnListOfPotions_whenGetAllPotionEndpointCalled_A1B2C3() throws Exception {
// Arrange
Potion potion1 = new Potion();
potion1.setId(UUID.randomUUID());
potion1.setName("Potion of Healing");
potion1.setDescription("Restores health");
// Assuming Potion model does not have setIngredients or getIngredients as per error
// potion1.setIngredients("Water, Herb");
Potion potion2 = new Potion();
potion2.setId(UUID.randomUUID());
potion2.setName("Potion of Strength");
potion2.setDescription("Increases strength");
// potion2.setIngredients("Blood, Root");
List<Potion> potions = Arrays.asList(potion1, potion2);
when(potionService.getAllPotion()).thenReturn(potions);
// Act & Assert
mockMvc.perform(get("/potions"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].name").value("Potion of Healing"))
.andExpect(jsonPath("$[1].name").value("Potion of Strength"));
verify(potionService).getAllPotion();
}
// Test generated using Keploy
@Test
void shouldReturnPotion_whenGetPotionByIdEndpointCalled_D3E4F5() throws Exception {
// Arrange
UUID potionId = UUID.fromString("8ab097b9-1a2f-46ab-8825-74313d9eb53c");
Potion potion = new Potion();
potion.setId(potionId);
potion.setName("Potion of Invisibility");
potion.setDescription("Makes the drinker invisible");
// Assuming Potion model does not have setIngredients as per error
// potion.setIngredients("Moon dew, Shadow silk");
when(potionService.getPotionById(potionId)).thenReturn(potion);
// Act & Assert
mockMvc.perform(get("/potions/" + potionId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(potionId.toString()))
.andExpect(jsonPath("$.name").value("Potion of Invisibility"))
.andExpect(jsonPath("$.description").value("Makes the drinker invisible"));
verify(potionService).getPotionById(potionId);
}
// Test generated using Keploy
@Test
void shouldCreatePotion_whenCreatePotionEndpointCalled_G5H6I7() throws Exception {
// Arrange
Potion potionToCreate = new Potion();
potionToCreate.setName("Mana Potion");
potionToCreate.setDescription("Restores mana");
// Assuming Potion model does not have setIngredients or getIngredients as per error
// potionToCreate.setIngredients("Crystal Water, Starflower");
Potion createdPotionWithId = new Potion();
UUID generatedId = UUID.randomUUID(); // This will be the ID set by the controller
createdPotionWithId.setId(generatedId);
createdPotionWithId.setName(potionToCreate.getName());
createdPotionWithId.setDescription(potionToCreate.getDescription());
// createdPotionWithId.setIngredients(potionToCreate.getIngredients());
// Capture the argument passed to the service to check if ID was set by controller
ArgumentCaptor<Potion> potionCaptor = ArgumentCaptor.forClass(Potion.class);
// We expect the service to be called with a Potion object that has an ID set by the controller.
// The service then returns this Potion object (or a representation of it after persistence).
when(potionService.createPotion(potionCaptor.capture())).thenAnswer(invocation -> {
Potion p = invocation.getArgument(0);
// Simulate service returning the potion with the ID it received
createdPotionWithId.setId(p.getId()); // Ensure the ID captured is used in the response
return createdPotionWithId;
});
// Act & Assert
mockMvc.perform(post("/potions")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(potionToCreate)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").exists()) // Check that an ID is present
.andExpect(jsonPath("$.name").value("Mana Potion"));
verify(potionService).createPotion(any(Potion.class));
Potion capturedPotion = potionCaptor.getValue();
assertThat(capturedPotion.getId()).isNotNull(); // ID should be set by controller before calling service
assertThat(capturedPotion.getName()).isEqualTo(potionToCreate.getName());
assertThat(capturedPotion.getDescription()).isEqualTo(potionToCreate.getDescription());
}
// Test generated using Keploy
@Test
void shouldUpdatePotion_whenUpdatePotionEndpointCalled_J7K8L9() throws Exception {
// Arrange
UUID potionId = UUID.fromString("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11");
Potion potionUpdateRequest = new Potion();
potionUpdateRequest.setName("Updated Elixir");
potionUpdateRequest.setDescription("Super strong");
// Assuming Potion model does not have setIngredients or getIngredients as per error
// potionUpdateRequest.setIngredients("Rare items");
Potion updatedPotionFromService = new Potion();
updatedPotionFromService.setId(potionId);
updatedPotionFromService.setName(potionUpdateRequest.getName());
updatedPotionFromService.setDescription(potionUpdateRequest.getDescription());
// updatedPotionFromService.setIngredients(potionUpdateRequest.getIngredients());
when(potionService.updatePotion(eq(potionId), any(Potion.class))).thenReturn(updatedPotionFromService);
// Act & Assert
mockMvc.perform(put("/potions/" + potionId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(potionUpdateRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(potionId.toString()))
.andExpect(jsonPath("$.name").value("Updated Elixir"))
.andExpect(jsonPath("$.description").value("Super strong"));
verify(potionService).updatePotion(eq(potionId), any(Potion.class));
}
}