Created
December 23, 2018 01:42
-
-
Save jcohen66/8d3ef8e4ef25d85b8cd19bd9450ad77a to your computer and use it in GitHub Desktop.
Spring Mock Test #spring #test #mock
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
import static org.hamcrest.Matchers.containsString; | |
import static org.mockito.Mockito.when; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
import com.katonahcomputing.springbasictests.Greeting.GreetingController; | |
import com.katonahcomputing.springbasictests.Greeting.GreetingService; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | |
import org.springframework.boot.test.mock.mockito.MockBean; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.web.servlet.MockMvc; | |
@RunWith(SpringRunner.class) | |
@WebMvcTest(GreetingController.class) | |
public class WebMockTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@MockBean | |
private GreetingService service; | |
@Test | |
public void greetingShouldReturnMessageFromService() throws Exception { | |
when(service.greet()).thenReturn("Hello Mock"); | |
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) | |
.andExpect(content().string(containsString("Hello Mock"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment