SyntaxHighlighter

Thursday, March 28, 2013

Spring Integration HTTP - Testing

Spring Integration HTTP - testing

with the new Spring 3.2, there's an even easier way to test Spring MVC and, in turn, Spring Integration HTTP endpoints; MockMvc.

here's a sample Spring Integration HTTP endpoint that, when invoked, logs the passed parameter.  (really quick and dirty)





 
 
 
 
 
 
 
 





now to test this

package de.incompleteco.spring.integration;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

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.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:/META-INF/spring/integration-context.xml"})
public class IntegrationContextTest {

 @Autowired
 private WebApplicationContext context;
 
 @Test
 public void test() throws Exception {
  //setup the mock mvc
  MockMvc mockMvc = webAppContextSetup(context).build();
  //execute
  mockMvc.perform(put("/log").content("something to log")).andExpect(status().isOk());
 }
 
}


No comments:

Post a Comment