test(hello): add integration test for CreateHello endpoint
Some checks failed
Quality Check / Checkstyle Main (pull_request) Successful in 36s
Quality Check / Tests (pull_request) Successful in 43s
gitea-sonarqube-bot ERROR
Build PR / Build and analyze (pull_request) Successful in 5m18s

This commit is contained in:
Jan Gleytenhoover 2024-10-02 13:05:44 +02:00
parent 66f3b0da4c
commit 0fb924f93d
Signed by: jank
GPG Key ID: B267751B8AE29EFE

@ -0,0 +1,50 @@
package de.szut.lf8_starter.integration.hello;
import de.szut.lf8_starter.hello.HelloRepository;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
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;
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
class CreateHelloTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private HelloRepository helloRepository;
@Test
void createProjectTest() throws Exception {
String content = """
{
"message": "test"
}
""";
final var contentAsString = this.mockMvc.perform(
post("/hello").content(content).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("message", is("test")))
.andReturn()
.getResponse()
.getContentAsString();
final var id = Long.parseLong(new JSONObject(contentAsString).get("id").toString());
final var hello = this.helloRepository.findById(id);
assertThat(hello.get().getMessage()).isEqualTo("test");
}
}