add integration test to create new user controle with sucess

This commit is contained in:
rayankonecny 2025-12-13 17:03:16 +00:00
parent 36ec46a2ea
commit 13ad4c8d45
2 changed files with 28 additions and 0 deletions

View file

@ -10,4 +10,6 @@ import org.springframework.stereotype.Repository;
public interface UserRepository extends MongoRepository<User, String> {
Optional<User> findByEmail(final String email);
void deleteByEmail(final String validEmail);
}

View file

@ -9,8 +9,12 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -18,6 +22,7 @@ import java.util.List;
import br.com.rayankonecny.userserviceapi.entity.User;
import br.com.rayankonecny.userserviceapi.repository.UserRepository;
import models.requests.CreateUserRequest;
@SpringBootTest
@AutoConfigureMockMvc
@ -67,7 +72,28 @@ public class UserControllerImpTest {
.andExpect(jsonPath("$[0].profiles").isArray());
userRepository.deleteAll(List.of(entity1,entity2));
}
@Test
void testSaveUserWithSuccess() throws Exception {
final var validEmail = "k42kak2ok@mail.com";
final var request = generateMock(CreateUserRequest.class).withEmail(validEmail);
mockMvc.perform(
post("/api/users")
.contentType(APPLICATION_JSON)
.content(toJson(request))
).andExpect(status().isCreated());
userRepository.deleteByEmail(validEmail);
}
private String toJson(final Object object) throws Exception {
try {
return new ObjectMapper().writeValueAsString(object);
} catch (final Exception e) {
throw new Exception("Error to convert object to json", e);
}
}
}