Files
spring-soap/spring-web-modules/spring-mvc-basics-4/src/test/java/com/baeldung/controller/GreetingsControllerUnitTest.java
timis1 b71857b923 JAVA-14723 Convert spring-mvc-basics-4 to Spring Boot project and upd… (#13399)
* JAVA-14723 Convert spring-mvc-basics-4 to Spring Boot project and update articles

* JAVA-14723 Refactoring and remove unused files

* JAVA-14723 remove the WEB-INF and migrate jsp to html

* JAVA-14723 Add back the removed test and rename the test into: ArticleViewerControllerWithRequiredAttributeIntegrationTest

---------

Co-authored-by: timis1 <noreplay@yahoo.com>
2023-02-20 23:45:47 +05:30

55 lines
1.9 KiB
Java

package com.baeldung.controller;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.validation.listvalidation.SpringListValidationApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringListValidationApplication.class)
public class GreetingsControllerUnitTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void givenReturnTypeIsString_whenJacksonOnClasspath_thenDefaultContentTypeIsJSON() throws Exception {
// Given
String expectedMimeType = "application/json";
// Then
String actualMimeType = this.mockMvc.perform(MockMvcRequestBuilders.get("/greetings-with-response-body", 1)).andReturn().getResponse().getContentType();
Assert.assertEquals(expectedMimeType, actualMimeType);
}
@Test
public void givenReturnTypeIsResponseEntity_thenDefaultContentTypeIsJSON() throws Exception {
// Given
String expectedMimeType = "application/json";
// Then
String actualMimeType = this.mockMvc.perform(MockMvcRequestBuilders.get("/greetings-with-response-entity", 1)).andReturn().getResponse().getContentType();
Assert.assertEquals(expectedMimeType, actualMimeType);
}
}