Created
November 28, 2013 10:01
-
-
Save oak-tree/7689675 to your computer and use it in GitHub Desktop.
mock testing with controller...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jpp.api.controller; | |
import org.apache.log4j.Logger; | |
import org.jpp.domain.Keyword; | |
import org.jpp.service.domain.KeywordService; | |
import org.junit.Assert; | |
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.setup.MockMvcBuilders; | |
import org.springframework.web.context.WebApplicationContext; | |
import static org.hamcrest.Matchers.*; | |
import static org.mockito.Mockito.*; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | |
/** | |
* @author oaktree The Class UniHelper Tester. seems that class name must end | |
* with "Test" | |
*/ | |
/** | |
* | |
* Integration tests for keyword controller see | |
* http://www.javacodegeeks.com/2013 | |
* /08/unit-testing-of-spring-mvc-controllers-rest-api.html for more info | |
* | |
* @author oaktree | |
* | |
*/ | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@WebAppConfiguration | |
@ContextConfiguration(value = "file:src/main/webapp/WEB-INF/applicationContext.xml") | |
public class KeywordControllerTest { | |
private static final Logger LOGGER = Logger | |
.getLogger(KeywordControllerTest.class); | |
/** The unicode service. */ | |
private MockMvc mockMvc; | |
// @Autowired | |
// private KeywordService serviceMock; | |
private KeywordService serviceMock; | |
@Autowired | |
private WebApplicationContext wac; | |
@Before | |
public void setup() { | |
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); | |
this.serviceMock = org.mockito.Mockito.mock(KeywordService.class); | |
} | |
public static String something() { | |
return "Hello World"; | |
} | |
@Test | |
public void testNothingKeywordController() { | |
Boolean ok = true; | |
Assert.assertTrue(ok); | |
} | |
@Test | |
public void testIndexJsp() throws Exception { | |
mockMvc.perform(get("/")) | |
.andExpect(status().isOk()); | |
} | |
@Test | |
public void testFindingOfKeyword() throws Exception { | |
Keyword first = new Keyword(); | |
first.setName("testing keyword1"); | |
first.setDescription("testing description 1"); | |
first.setId(1); | |
when(serviceMock.findById(new Long(1))).thenReturn(first); | |
mockMvc.perform(get("/keyword/13")) | |
.andExpect(status().isOk()) | |
.andExpect(content().contentType(TestUtil.APPLICATION_JSON_UTF8)) | |
.andExpect(jsonPath("$", hasSize(1))) | |
.andExpect(jsonPath("$.id", is(1))) | |
.andExpect( | |
jsonPath("$.description", is("testing description 1"))) | |
.andExpect(jsonPath("$.name", is("testing keyword1"))); | |
verify(serviceMock, times(1)).findById(new Long(1)); | |
verifyNoMoreInteractions(serviceMock); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment