diff --git a/spring-boot-modules/spring-boot-mvc-3/pom.xml b/spring-boot-modules/spring-boot-mvc-3/pom.xml
index 43a492786e..f2b6c129f8 100644
--- a/spring-boot-modules/spring-boot-mvc-3/pom.xml
+++ b/spring-boot-modules/spring-boot-mvc-3/pom.xml
@@ -40,6 +40,18 @@
commons-io
${commons-io.version}
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+ /src/main/resources
+
+
+
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java
index ab233a8b60..218be68a45 100644
--- a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java
@@ -1,9 +1,12 @@
package com.baeldung.produceimage.controller;
import org.apache.commons.io.IOUtils;
+import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
@@ -29,6 +32,18 @@ public class DataProducerController {
return IOUtils.toByteArray(in);
}
+ @GetMapping("/get-image-dynamic-type")
+ @ResponseBody
+ public ResponseEntity getImageDynamicType(@RequestParam("jpg") boolean jpg) {
+ final MediaType contentType = jpg ? MediaType.IMAGE_JPEG : MediaType.IMAGE_PNG;
+ final InputStream in = jpg ?
+ getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg") :
+ getClass().getResourceAsStream("/com/baeldung/produceimage/image.png");
+ return ResponseEntity.ok()
+ .contentType(contentType)
+ .body(new InputStreamResource(in));
+ }
+
@GetMapping(value = "/get-file", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] getFile() throws IOException {
final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/data.txt");
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt
new file mode 100644
index 0000000000..5a8297fc5e
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/data.txt
@@ -0,0 +1 @@
+Hello Baeldung!
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg
new file mode 100644
index 0000000000..db3abce0d6
Binary files /dev/null and b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.jpg differ
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png
new file mode 100644
index 0000000000..16d218e334
Binary files /dev/null and b/spring-boot-modules/spring-boot-mvc-3/src/main/resources/com/baeldung/produceimage/image.png differ
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java
new file mode 100644
index 0000000000..29f794645a
--- /dev/null
+++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/produceimage/DataProducerControllerIntegrationTest.java
@@ -0,0 +1,46 @@
+package com.baeldung.produceimage;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+import org.springframework.web.context.WebApplicationContext;
+
+@SpringBootTest(classes = ImageApplication.class,
+ webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+public class DataProducerControllerIntegrationTest {
+
+ @Autowired
+ private WebApplicationContext webApplicationContext;
+
+ private MockMvc mockMvc;
+
+ @BeforeEach
+ public void setup() {
+ this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
+ }
+
+ @Test
+ void givenJpgTrue_whenGetImageDynamicType_ThenContentTypeIsJpg() throws Exception {
+ mockMvc.perform(get("/get-image-dynamic-type?jpg=true"))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType(MediaType.IMAGE_JPEG))
+ .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_JPEG_VALUE));
+ }
+
+ @Test
+ void givenJpgFalse_whenGetImageDynamicType_ThenContentTypeIsFalse() throws Exception {
+ mockMvc.perform(get("/get-image-dynamic-type?jpg=false"))
+ .andExpect(status().isOk())
+ .andExpect(content().contentType(MediaType.IMAGE_PNG))
+ .andExpect(header().stringValues(HttpHeaders.CONTENT_TYPE, MediaType.IMAGE_PNG_VALUE));
+ }
+
+}
diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountIntegrationTest.java
similarity index 84%
rename from spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java
rename to spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountIntegrationTest.java
index 507321bf8e..7c23153951 100644
--- a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountUnitTest.java
+++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/springvalidation/UserAccountIntegrationTest.java
@@ -1,9 +1,7 @@
package com.baeldung.springvalidation;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -15,7 +13,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
-public class UserAccountUnitTest {
+public class UserAccountIntegrationTest {
@Autowired
private MockMvc mockMvc;
@@ -46,10 +44,8 @@ public class UserAccountUnitTest {
public void givenSaveBasicInfoStep1_whenIncorrectInput_thenError() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.post("/saveBasicInfoStep1")
.accept(MediaType.TEXT_HTML))
- // .param("name", "test123")
- // .param("password", "pass"))
.andExpect(model().errorCount(2))
- // .andExpect(view().name("error"))
+ .andExpect(view().name("error"))
.andExpect(status().isOk())
.andDo(print());
}