Skip to content

Commit

Permalink
Merge pull request eugenp#8851 from albanoj2/BAEL-3871
Browse files Browse the repository at this point in the history
BAEL-3871: Added tests for duplicate HTTP controller methods
  • Loading branch information
eric-martin authored Mar 15, 2020
2 parents 3f09669 + c87f032 commit c8dce0e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.baeldung.requestmapping;

import org.springframework.http.HttpStatus;
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.PathVariable;
Expand Down Expand Up @@ -103,8 +105,8 @@ public String putAndPostFoos() {
// --- Ambiguous Mapping

@GetMapping(value = "foos/duplicate" )
public String duplicate() {
return "Duplicate";
public ResponseEntity<String> duplicate() {
return new ResponseEntity<>("Duplicate", HttpStatus.OK);
}

// uncomment for exception of type java.lang.IllegalStateException: Ambiguous mapping
Expand All @@ -114,14 +116,14 @@ public String duplicate() {
// return "Duplicate";
// }

@GetMapping(value = "foos/duplicate/xml", produces = MediaType.APPLICATION_XML_VALUE)
public String duplicateXml() {
return "Duplicate Xml";
@GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<String> duplicateXml() {
return new ResponseEntity<>("<message>Duplicate</message>", HttpStatus.OK);
}

@GetMapping(value = "foos/duplicate/json", produces = MediaType.APPLICATION_JSON_VALUE)
public String duplicateJson() {
return "Duplicate Json";
@GetMapping(value = "foos/duplicate", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> duplicateJson() {
return new ResponseEntity<>("{\"message\":\"Duplicate\"}", HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.requestmapping;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@WebMvcTest(FooMappingExamplesController.class)
public class FooMappingExamplesControllerUnitTest {

@Autowired
private MockMvc mvc;

@Test
public void givenAcceptsJson_whenGetDuplicate_thenJsonResponseReturned() throws Exception {
mvc.perform(get("/ex/foos/duplicate")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{\"message\":\"Duplicate\"}"));
}

@Test
public void givenAcceptsXml_whenGetDuplicate_thenXmlResponseReturned() throws Exception {
mvc.perform(get("/ex/foos/duplicate")
.accept(MediaType.APPLICATION_XML))
.andExpect(status().isOk())
.andExpect(content().string("<message>Duplicate</message>"));
}
}

0 comments on commit c8dce0e

Please sign in to comment.