Skip to content

Commit

Permalink
feat : adds more Integration Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Mar 10, 2024
1 parent 90f3ca1 commit 3b66ecb
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 29 deletions.
42 changes: 21 additions & 21 deletions httpClients/boot-rest-template/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<java.version>21</java.version>
<springdoc-openapi.version>2.3.0</springdoc-openapi.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
<spotless.version>2.43.0</spotless.version>
<dependency-check-maven.version>9.0.9</dependency-check-maven.version>
Expand Down Expand Up @@ -331,25 +331,25 @@
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
43 changes: 43 additions & 0 deletions httpClients/boot-restclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,49 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<runOrder>alphabetical</runOrder>
<reportsDirectory>${junit.utReportFolder}</reportsDirectory>
<excludes>
<exclude>**/*IT*</exclude>
<exclude>**/*IntTest*</exclude>
<exclude>**/*IntegrationTest*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<classesDirectory>${project.build.outputDirectory}</classesDirectory>
<runOrder>alphabetical</runOrder>
<reportsDirectory>${junit.itReportFolder}</reportsDirectory>
<includes>
<include>**/*IT*</include>
<include>**/*IntTest*</include>
<include>**/*IntegrationTest*</include>
</includes>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ public record PostDto(
Long userId,
Long id,
@NotBlank(message = "title can't be blank") String title,
String body) {
public PostDto withId(Long id) {
return new PostDto(userId(), id, title(), body());
}
}
String body) {}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package com.example.restclient.bootrestclient.web.controllers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
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;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.example.restclient.bootrestclient.model.response.PostDto;
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.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest
@AutoConfigureMockMvc
class PostControllerIntTest {

@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;

@Test
void shouldFindPostById() throws Exception {
Expand All @@ -36,4 +43,73 @@ void shouldFindPostById() throws Exception {
is(
"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto")));
}

@Test
void shouldCreateNewPost() throws Exception {
PostDto postDto = new PostDto(1L, null, "First Title", "First Body");
this.mockMvc
.perform(
post("/api/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postDto)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.title", is(postDto.title())))
.andExpect(jsonPath("$.body", is(postDto.body())))
.andExpect(jsonPath("$.userId", is(postDto.userId()), Long.class));
}

@Test
void shouldReturn400WhenCreateNewPostWithoutTitle() throws Exception {
PostDto post = new PostDto(1L, null, null, "First Body");

this.mockMvc
.perform(
post("/api/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(post)))
.andExpect(status().isBadRequest())
.andExpect(
header().string(
HttpHeaders.CONTENT_TYPE,
is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("about:blank")))
.andExpect(jsonPath("$.title", is("Constraint Violation")))
.andExpect(jsonPath("$.status", is(400)))
.andExpect(jsonPath("$.detail", is("Invalid request content.")))
.andExpect(jsonPath("$.instance", is("/api/posts")))
.andExpect(jsonPath("$.violations", hasSize(1)))
.andExpect(jsonPath("$.violations[0].field", is("title")))
.andExpect(jsonPath("$.violations[0].message", is("title can't be blank")))
.andReturn();
}

@Test
void shouldUpdatePost() throws Exception {
PostDto postDto = new PostDto(1L, 1L, "First Title", "First Body");

this.mockMvc
.perform(
put("/api/posts/{id}", postDto.id())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postDto)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(postDto.id()), Long.class))
.andExpect(jsonPath("$.title", is(postDto.title())))
.andExpect(jsonPath("$.body", is(postDto.body())))
.andExpect(jsonPath("$.userId", is(postDto.userId()), Long.class));
}

@Test
void shouldDeletePost() throws Exception {

String response =
this.mockMvc
.perform(delete("/api/posts/{id}", 50))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
assertThat(response).isEqualTo("{}");
}
}

0 comments on commit 3b66ecb

Please sign in to comment.