Created
April 25, 2018 22:51
-
-
Save denispeyrusaubes/5f64852c073d7efa6ff2bbeecae84afb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 com.retengr.exercice3; | |
import com.retengr.main.Main; | |
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.boot.test.context.SpringBootTest; | |
import org.springframework.http.MediaType; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.context.web.WebAppConfiguration; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.MvcResult; | |
import org.springframework.web.context.WebApplicationContext; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | |
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; | |
import java.nio.charset.Charset; | |
import java.util.List; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest(classes = {Main.class}) | |
@WebAppConfiguration | |
public class Exercice3ApplicationTests { | |
private MediaType contentType = new MediaType(MediaType.TEXT_PLAIN.getType(), | |
MediaType.TEXT_PLAIN.getSubtype(), | |
Charset.forName("ISO-8859-1")); | |
private MediaType contentType_json = new MediaType(MediaType.APPLICATION_JSON.getType(), | |
MediaType.APPLICATION_JSON.getSubtype(), | |
Charset.forName("utf8")); | |
private MockMvc mockMvc; | |
@Autowired | |
private WebApplicationContext webApplicationContext; | |
@Before | |
public void setup() throws Exception { | |
this.mockMvc = webAppContextSetup(webApplicationContext).build(); | |
} | |
@Test | |
public void testHello() throws Exception { | |
MvcResult result = mockMvc.perform(get("/hello?name=Denis")) | |
.andExpect(status().isOk()) | |
.andExpect(content().contentType(contentType)) | |
.andReturn(); | |
Assert.assertEquals(result.getResponse().getContentAsString(),"Hello Denis"); | |
} | |
@Test | |
public void testListClient() throws Exception { | |
mockMvc.perform(get("/banque/clients")) | |
.andExpect(status().isOk()) | |
.andExpect(content().contentType(contentType_json)) | |
.andExpect(content().json("[{\"id\":0,\"nom\":\"Durand\",\"prenom\":\"Pierre\"},{\"id\":1,\"nom\":\"Dupond\",\"prenom\":\"Paul\"}]")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment