Google

Mar 15, 2013

Spring MVC Test -- Testing the Spring MVC Controllers

In the previous post entitled "Unit testing Spring MVC controllers for web and RESTful services with spring-test-mvc" I covered examples for an HTTP get. In this post, I will show an example of testing an HTTP POST that takes "JSON data" as the body. Here is the code snippet.

public class MyAppControllerTest {

 private MyAppService mockMyAppService;
 private MyAppController controller;

 @Before
 public void setup() {
   controller = new MyAppController();
   mockMyAppService = mock(MyAppServiceImpl.class);
   controller.setMyAppService(mockMyAppService);
 }
 
 @Test
 public void testAddOrModifyAdjustment() throws Exception {
     //mock the service call within the controller.
  when(mockMyAppService.addOrModifyAdjustment((MyAppDetail) Mockito.any())).thenReturn(getMyAppDetail());

  standaloneSetup(controller)
    .build()
    .perform(
      MockMvcRequestBuilders.post("/addOrModifyAdjustment").contentType(MediaType.APPLICATION_JSON)
        .body(getAdjustmentDetailJsonString().getBytes()).accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk()).andExpect(content().type("application/json"))
    .andExpect(jsonPath("MyAppId").value(874))
    .andExpect(jsonPath("MyAppDetailId").value(48515))
    .andExpect(jsonPath("portfolioCd").value("3927"))
    .andExpect(jsonPath("txnCd").value("ADJUSTMNT"))
    .andExpect(jsonPath("txnTypeCd").value("59"))
    .andExpect(jsonPath("txnTypeDesc").value("PURCHASE"))
    .andExpect(jsonPath("cashValue").value(1100000.02))
    .andExpect(jsonPath("sourceUnits").value(200.00))
    
 }

 private String getAdjustmentDetailJsonString(){
  String jsonStr =
  "{\r\n" + 
  "  \"MyAppId\":874,\r\n" + 
  "  \"MyAppDetailId\":48515,\r\n" + 
  "        \"portfolioCd\": \"3927\",\r\n" + 
  "  \"currencyCd\": \"AUD\",\r\n" + 
  "        \"accountCd\": \"CURR-AUD\",\r\n" + 
  "  \"txnCd\":\"ADJUSTMNT\",\r\n" + 
  "  \"txnTypeCd\":\"59\",\r\n" + 
  "  \"txnTypeDesc\":\"PURCHASE\",\r\n" + 
  "  \"cashValue\": 1100000.02000000,\r\n" + 
  "        \"sourceUnits\":200.00,\r\n" + 
  "}";
  
  return jsonStr;
 }
  
 private MyAppDetail getMyAppDetail() {
  MyAppDetail cfDetail = new MyAppDetail();
  cfDetail.setMyAppId(BigInteger.valueOf(874));
  cfDetail.setMyAppDetailId(BigInteger.valueOf(48515));
  cfDetail.setPortfolioCd("3927");
  cfDetail.setTxnCd("ADJUSTMNT");
  cfDetail.setTxnTypeCd("59");
  cfDetail.setTxnTypeDesc("PURCHASE");
  cfDetail.setPositionIndicator("OUT");
  return cfDetail;
 }
}



The actual Spring MVC controller that is being tested will be defined as

@Controller
public class MyAppController {

    @Resource(name = "myAppService")
 private MyAppService myAppService;

 private static final Logger logger = LoggerFactory.getLogger(CashForecastController.class);

    @RequestMapping(value = "/addOrModifyAdjustment", method = RequestMethod.POST,  headers="Accept=application/json")
 public @ResponseBody MyAppDetail addOrModifyAdjustment(@RequestBody MyAppDetail adjDetail) throws Exception 
 {
  logger.info("adjustMyAppDetail cfDetail={}, MyApp={}", new Object[] {adjDetail});
  
  MyAppDetail addOrModifyAdjustment = myAppService.addOrModifyAdjustment(adjDetail);

  return addOrModifyAdjustment;
 }
 
}

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home