Spring RestController + Junit Testing

Joschi

I'm playing around with spring-test of springframework. My intention is to test the following POST method in my rest controller:

@RestController
@RequestMapping("/project")
public class ProjectController {

  @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  public Project createProject(@RequestBody Project project, HttpServletResponse response) {
    // TODO: create the object, store it in db...
    response.setStatus(HttpServletResponse.SC_CREATED);
    // return the created object - simulate by returning the request.
    return project;
  }
}

This is my test case:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ProjectController.class })
@WebAppConfiguration
public class ProjectControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testCreationOfANewProjectSucceeds() throws Exception {
        Project project = new Project();
        project.setName("MyName");
        String json = new Gson().toJson(project);

        mockMvc.perform(
                post("/project")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(json))
                .andExpect(status().isCreated());
    }

}

When I execute it I get Status Code 415 instead of 201. What am I missing? A simple GET request works.

rajadilipkolli

You need to add annotation @EnableWebMvc for @RestController to work, this is missing from your code, adding this will solve the issue

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring RestController + Junit Testing

From Dev

(Context) Problems with @RestController testing with JUnit4 and Spring

From Dev

Spring Hibernate Repository Testing with jUnit

From Dev

Spring MVC + Shiro + Junit Testing

From Dev

Spring JUnit testing using mockMvc

From Dev

Spring MVC + Shiro + Junit Testing

From Dev

Spring JUnit testing using mockMvc

From Dev

Testing Spring JDBC with JMockit with JUnit4

From Dev

How to testing Spring validator with Junit and Mockito

From Dev

Spring with JUnit Testing and Dependency Injection does not work

From Dev

JUnit testing of spring boot @value variable

From Dev

How to testing Spring validator with Junit and Mockito

From Dev

Testing with Junit 5 failed in Spring boot

From Dev

Spring @RestController

From Dev

Testing @RestController with @ControllerAdvice

From Dev

JUnit-testing a Spring @Async void service method

From Dev

JUnit Testing (with Spring MVC and Hibernate): IllegalArgumentException: Unknown entity

From Java

Spring MVC @RestController and redirect

From Dev

Spring retry not working in RestController

From Dev

Spring MVC RestController scope

From Dev

Spring ExceptionHandler in RestController

From Dev

Spring Boot @RestController and @Controller

From Dev

Testing @Validated RestController in standalone MockMvc not working

From Dev

Spock testing for SpringBoot RestController and mocking of exceptions

From Dev

JUnit testing with Maven

From Dev

JUnit testing custom exception

From Dev

Cucumber and JUnit Testing

From Dev

Using Maven for JUnit testing

From Dev

Junit testing stack pop