-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8b8cb4b
commit f38015f
Showing
7 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/main/java/com/example/intranetbackend/dto/UserRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/main/java/com/example/intranetbackend/dto/UserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/test/java/com/example/intranetbackend/IntranetBackendApplicationTests.java
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
src/test/java/com/example/intranetbackend/controller/UserControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.example.intranetbackend.controller; | ||
|
||
import com.example.intranetbackend.domain.User; | ||
import com.example.intranetbackend.dto.UserRequest; | ||
import com.example.intranetbackend.dto.UserResponse; | ||
import com.example.intranetbackend.service.UserService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@WebMvcTest(UserController.class) | ||
class UserControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private UserService userService; | ||
|
||
@Test | ||
void testGetUser() throws Exception { | ||
// Arrange | ||
UserResponse userResponse = new UserResponse("tester", "password", null); | ||
when(userService.getUsers()).thenReturn(Collections.singletonList(userResponse)); | ||
|
||
// Act & Assert | ||
mockMvc.perform(MockMvcRequestBuilders.get("/api/user/findAll") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(1)) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].username").value("tester")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].password").value("password")); | ||
} | ||
|
||
@Test | ||
void testSetUser() throws Exception { | ||
// Arrange | ||
UserRequest userRequest = new UserRequest("tester", "password"); | ||
|
||
// Mocking the service method call | ||
when(userService.setUser(any(UserRequest.class))).thenReturn(new User()); | ||
|
||
// Act & Assert | ||
mockMvc.perform(MockMvcRequestBuilders.post("/api/user/add") | ||
.content(asJsonString(userRequest)) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()); | ||
} | ||
|
||
private static String asJsonString(final Object obj) { | ||
try { | ||
return new ObjectMapper().writeValueAsString(obj); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
58 changes: 58 additions & 0 deletions
58
src/test/java/com/example/intranetbackend/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.example.intranetbackend.service; | ||
|
||
import com.example.intranetbackend.domain.User; | ||
import com.example.intranetbackend.dto.UserRequest; | ||
import com.example.intranetbackend.dto.UserResponse; | ||
import com.example.intranetbackend.repository.UserRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@SpringBootTest | ||
class UserServiceTest { | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@InjectMocks | ||
private UserService userService; | ||
|
||
@Test | ||
void testSetUser() { | ||
// Arrange | ||
UserRequest userRequest = new UserRequest("tester", "password"); | ||
// Mocking the repository save method | ||
Mockito.when(userRepository.save(Mockito.any(User.class))).thenReturn(new User()); | ||
// Act | ||
User savedUser = userService.setUser(userRequest); | ||
// Assert | ||
assertNotNull(savedUser); | ||
|
||
} | ||
|
||
|
||
@Test | ||
void testGetUsers() { | ||
// Arrange | ||
UserResponse userResponse = new UserResponse("tester", "password", null); | ||
List<UserResponse> userResponses = Arrays.asList(userResponse); | ||
// Mocking the repository method call | ||
Mockito.when(userRepository.findUsernameAndPasswordAndFecha()).thenReturn(userResponses); | ||
|
||
// Act | ||
List<UserResponse> result = userService.getUsers(); | ||
|
||
// Assert | ||
assertEquals(1, result.size()); | ||
assertEquals("tester", result.get(0).getUsername()); | ||
assertEquals("password", result.get(0).getPassword()); | ||
} | ||
} |