Skip to content

Instantly share code, notes, and snippets.

@hanrw
Last active November 17, 2016 02:58
Show Gist options
  • Save hanrw/202ed1181631fb1ec1573127be813ed6 to your computer and use it in GitHub Desktop.
Save hanrw/202ed1181631fb1ec1573127be813ed6 to your computer and use it in GitHub Desktop.
Spring mvc test sample
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="xxxxx"/>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
@RestController
public class PlayerController {
@RequestMapping(value = "/authenticate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
public @ResponseBody PlayerAuthenticationResponse authenticate(@RequestBody final PlayerAuthentication request) {
return new PlayerAuthenticationResponse()
}
@RequestMapping(value = "/getBalance", method = [RequestMethod.POST])
public @ResponseBody BalanceResponse getBalance(@RequestBody final Balance request) {
return new BalanceResponse()
}
}
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.http.MediaType
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.setup.MockMvcBuilders
import javax.xml.bind.JAXBContext
import javax.xml.bind.Marshaller
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
class PlayerControllerTest {
private MockMvc mockMvc
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.standaloneSetup(new PlayerController()).defaultRequest(get("/")).build()
}
@Test
public void should_return_player_authentication_response() throws Exception {
def postData = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns3:playerAuthentication xmlns:ns2="http://williamsinteractive.com/integration/vanilla/api/common" xmlns:ns3="http://williamsinteractive.com/integration/vanilla/api/player"><accountRef>some-accountRef</accountRef><context>some-context</context><gameCode>some-code</gameCode><ticket>some-ticket</ticket></ns3:playerAuthentication>'
def exceptedResult = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:playerAuthenticationResponse xmlns:ns2="http://williamsinteractive.com/integration/vanilla/api/player"><balance>0</balance></ns2:playerAuthenticationResponse>'
def post = MockMvcRequestBuilders.post("/authenticate")
.contentType(MediaType.APPLICATION_XML)
.content(postData)
mockMvc.perform(post)
.andExpect(status().isOk()).andExpect(content().string(exceptedResult))
}
@Test
public void should_return_balance_response() throws Exception {
def postData = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:balance xmlns:ns2="http://williamsinteractive.com/integration/vanilla/api/player"><accountRef>some-accountRef</accountRef><context>some-context</context><currency>some-currency</currency><gameCode>some-code</gameCode><ticket>some-ticket</ticket></ns2:balance>'
def exceptedResult = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:balanceResponse xmlns:ns2="http://williamsinteractive.com/integration/vanilla/api/player"><balance>0</balance></ns2:balanceResponse>'
def post = MockMvcRequestBuilders.post("/getBalance")
.contentType(MediaType.APPLICATION_XML)
.content(postData)
mockMvc.perform(post)
.andExpect(status().isOk()).andExpect(content().string(exceptedResult))
}
def printXmlForPreparePostData() {
JAXBContext jc = JAXBContext.newInstance(Balance.class)
Marshaller m = jc.createMarshaller()
def balance = new Balance()
balance.accountRef = 'some-accountRef'
balance.currency = new Currency('some-currency', 0, 0)
balance.ticket = 'some-ticket'
balance.gameCode = 'some-code'
balance.context = 'some-context'
m.marshal(balance, System.out)
}
}
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.EnableWebMvc
@Configuration
@EnableWebMvc
public class WebConfig {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment