Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion spring-web-lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
## Sources 🔗

* [Java Senior Quiz 🧩](https://youtube.com/playlist?list=PLO5muTI694A5yRrEDNY1czqEUVbYHG_AH&si=KteWFowVcOSTQ8F-)
* [Action Jackson! Effective JSON processing in Spring Boot Applications by Joris Kuipers @ Spring I/O](https://youtu.be/BhiF6e24l5k?si=IxS0X6FtgfWzdP-c)
* [Action Jackson! Effective JSON processing in Spring Boot Applications by Joris Kuipers @ Spring I/O](https://youtu.be/BhiF6e24l5k?si=IxS0X6FtgfWzdP-c)
* [Spring Boot testing: Zero to Hero by Daniel Garnier-Moiroux](https://youtu.be/u5foQULTxHM?si=WPZKUZY-DMqcHGdG)
4 changes: 0 additions & 4 deletions spring-web-lab/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pl.mperor.lab.spring.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/hello")
@RestController
class HelloController {

@GetMapping
String hello() {
return "🗺️ Hello World!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pl.mperor.lab.spring;

import org.assertj.core.api.Assertions;
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.client.MockMvcClientHttpRequestFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.web.client.RestClient;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
public class SpringMockMvcTest {

@Autowired
private MockMvc mvc;
@Autowired
private MockMvcTester tester;

@Test
public void shouldReturnHealthStatusUp() throws Exception {
mvc.perform(get("/actuator/health"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("UP"));

var restClientBody = RestClient.builder()
.requestFactory(new MockMvcClientHttpRequestFactory(mvc)).build()
.get()
.uri("/actuator/health")
.retrieve()
.body(String.class);
Assertions.assertThat(restClientBody).contains("{\"status\":\"UP\"}");

tester.get().uri("/actuator/health")
.exchange()
.assertThat()
.hasStatusOk()
.bodyJson().extractingPath("$.status").isEqualTo("UP");
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
package pl.mperor.lab.spring;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
@SpringBootTest
public class SpringWebLabApplicationTest {

@Autowired
private WebTestClient client;

@Test
public void contextLoads() {
}

@Test
public void shouldReturnHealthStatusUp() {
client.get().uri("/actuator/health")
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK)
.expectBody()
.jsonPath("$.status").isEqualTo("UP");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pl.mperor.lab.spring;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.client.RestClient;

import java.net.URI;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringWithTomcatTest {

@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;

@Test
public void shouldReturnHealthStatusUp() {
var body = RestClient.create()
.get()
.uri("http://localhost:{port}/actuator/health", port)
.retrieve()
.body(String.class);
Assertions.assertThat(body).contains("{\"status\":\"UP\"}");

var restTemplateBody = restTemplate.getForObject(URI.create("/actuator/health"), String.class);
Assertions.assertThat(restTemplateBody).contains("{\"status\":\"UP\"}");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.mperor.lab.spring.controller;

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.test.web.servlet.assertj.MockMvcTester;

@WebMvcTest
class HelloControllerTest {

@Autowired
MockMvcTester tester;

@Test
void shouldReturnHelloWorld() {
tester.get()
.uri("/hello")
.exchange()
.assertThat().hasStatusOk()
.bodyText().isEqualTo("🗺️ Hello World!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class JacksonTest {

@Autowired
private JacksonTester<Sample> sampleTester;

@Autowired
private JacksonTester<Map<String, Sample>> wrappedTester;

Expand All @@ -36,7 +37,7 @@ void testSampleMarshalling() throws IOException {
Assertions.assertThat(wrappedTester.write(wrapped)).isEqualToJson("""
{
"sample" : {
"integer" : 1,
"integer" : 1,
"string" : "Text"
}
}
Expand Down
Loading