Skip to content

Commit

Permalink
Merge pull request #185 from JuniorsJava/#182-fix-return-format
Browse files Browse the repository at this point in the history
#182: add default json format
  • Loading branch information
romach committed Mar 12, 2016
2 parents ce29f3b + 7f5859f commit 285dc13
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
6 changes: 5 additions & 1 deletion restservice/src/main/resources/mvc-dispatcher-servlet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="org.itevents.controller"/>
<mvc:annotation-driven />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/json" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>

<bean class="org.itevents.controller.swagger.SwaggerCustomConfig"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.Arrays;

/**
* Created by ramax on 11/10/15.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public void init() {
@Test
public void shouldFindEventById() throws Exception {
Event event = BuilderUtil.buildEventJava();
String expectedEventInJson = new ObjectMapper().writeValueAsString(event);

when(eventService.getEvent(event.getId())).thenReturn(event);

mockMvc.perform(get("/events/" + event.getId()))
.andExpect(status().isOk());
mockMvc.perform(get("/events/" + event.getId())
.header("Accept", "application/json"))
.andExpect(status().isOk())
.andExpect(content().json(expectedEventInJson));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.itevents.integration;

import org.codehaus.jackson.map.ObjectMapper;
import org.itevents.dao.model.Event;
import org.itevents.service.EventService;
import org.itevents.test_utils.BuilderUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;

import static org.mockito.Mockito.when;
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;

/**
* Created by ramax on 3/10/16.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml",
"classpath:applicationContextTestAddon.xml",
"classpath:mvc-dispatcher-servlet.xml",
"classpath:spring-security.xml"})
@WebAppConfiguration
public class ControllerTest {
@Inject
private WebApplicationContext context;
private MockMvc mvc;
@Inject
private EventService eventService;

@Before
public void setup() {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
}

@Test
public void shouldGetJsonFormatEvent() throws Exception {
Event event = BuilderUtil.buildEventJava();
String expectedEventInJson = new ObjectMapper().writeValueAsString(event);

when(eventService.getEvent(event.getId())).thenReturn(event);

mvc.perform(get("/events/" + event.getId()))
.andExpect(content().json(expectedEventInJson))
.andExpect(status().isOk());
}
}

0 comments on commit 285dc13

Please sign in to comment.