This Spring controller unit test is based on
HATEOAS RESTFul Service with Spring tutorial. The request URL and JSON response content are shown below.
REQUEST URL: http://localhost:8080/simpleSpring/greeting?name=John
RESPONSE JSON: {“content”:”Hello, John!”,”links”:[{“rel”:”self”,”href”:”http://localhost:8080/simpleSpring/greeting?name=John”}]}
REQUEST URL: http://localhost:8080/simpleSpring/greeting
RESPONSE JSON:{“content”:”Hello, World!”,”links”:[{“rel”:”self”,”href”:”http://localhost:8080/simpleSpring/greeting?name=World”}]}
The first step is to have the relevant jars for testing in the pom.xml file.
Step 1: The pom.xml for the testing.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simpleSpringRestWeb</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>simpleSpringRestWeb</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <slf4j.version>1.7.10</slf4j.version> <commons-logging.version>1.1.1</commons-logging.version> </properties> <dependencies> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- Mockito --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.0</version> <scope>test</scope> </dependency> <!-- Hamcrest --> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <!-- Spring test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.0.9.RELEASE</version> <scope>test</scope> </dependency> <!-- JSON test --> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <version>0.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path-assert</artifactId> <version>0.8.1</version> <scope>test</scope> </dependency> //... othe Spring, logging depenedncies </dependencies> </project> |
Step 2: The unit testing
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.mytutorial; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.web.context.WebApplicationContext; @WebAppConfiguration @ContextConfiguration( classes = SimpleConfig.class) @RunWith(SpringJUnit4ClassRunner.class) public class HelloControllerImplTest { private MockMvc mockMvc; @Autowired private WebApplicationContext applicationContext; @Before public void setUp() throws Exception { mockMvc = webAppContextSetup(applicationContext).build(); } @Test public void testSayHello() throws Exception { mockMvc.perform(MockMvcRequestBuilders .get("/greeting")).andExpect(MockMvcResultMatchers.status() .isOk()); } @Test public void testSayHello2() throws Exception { mockMvc.perform(MockMvcRequestBuilders .get("/greeting/?name=John")).andExpect(MockMvcResultMatchers.status() .isOk()) .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) .andExpect(MockMvcResultMatchers.jsonPath("$.content", Matchers.is("Hello, John!"))) .andExpect(MockMvcResultMatchers.jsonPath("$.links[0].href", Matchers.is("http://localhost/greeting?name=John"))); } } |
Step 3: The Spring MVC controller class being tested
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.mytutorial; //linkTo & methodOn static methods are from ControllerLinkBuilder import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloControllerImpl { private static final String FORMAT = "Hello, %s!"; @RequestMapping("/greeting") @ResponseBody public HttpEntity<Hello> sayHello(@RequestParam( value = "name", required = false, defaultValue = "World") String name) { Hello hello = new Hello(String.format(FORMAT, name)); hello.add(linkTo(methodOn(HelloControllerImpl.class).sayHello(name)).withSelfRel()); return new ResponseEntity<Hello>(hello, HttpStatus.OK); } } |