test: Increase test coverage #33
					 2 changed files with 74 additions and 0 deletions
				
			
		| 
						 | 
					@ -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");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,24 @@
 | 
				
			||||||
 | 
					package de.szut.lf8_starter.integration.welcome;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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.test.web.servlet.MockMvc;
 | 
				
			||||||
 | 
					import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
 | 
				
			||||||
 | 
					import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
 | 
				
			||||||
 | 
					import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@SpringBootTest
 | 
				
			||||||
 | 
					@AutoConfigureMockMvc
 | 
				
			||||||
 | 
					class WelcomeTest {
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private MockMvc mockMvc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void welcomeTest() throws Exception {
 | 
				
			||||||
 | 
					        mockMvc.perform(get("/welcome"))
 | 
				
			||||||
 | 
					                .andExpect(status().isOk())
 | 
				
			||||||
 | 
					                .andExpect(content().string("welcome to lf8_starter"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in a new issue