4 changed files with 85 additions and 10 deletions
Unified View
Diff Options
-
6UB_UserServiceProxy/pom.xml
-
4UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/controller/UserController.java
-
8UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/model/User.java
-
77UB_UserServiceProxy/src/test/java/com/example/urbanbazaar/controller/UserControllerTest.java
@ -0,0 +1,77 @@ |
|||||
|
package com.example.urbanbazaar.controller; |
||||
|
|
||||
|
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; |
||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
||||
|
|
||||
|
import org.junit.Before; |
||||
|
import org.junit.FixMethodOrder; |
||||
|
import org.junit.Test; |
||||
|
import org.junit.runner.RunWith; |
||||
|
import org.junit.runners.MethodSorters; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.http.MediaType; |
||||
|
import org.springframework.test.context.ContextConfiguration; |
||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
|
import org.springframework.test.web.servlet.MockMvc; |
||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
||||
|
import org.springframework.web.context.WebApplicationContext; |
||||
|
|
||||
|
import com.example.urbanbazaar.UbUserServiceProxyApplication; |
||||
|
|
||||
|
@RunWith(SpringJUnit4ClassRunner.class) |
||||
|
@ContextConfiguration(classes = UbUserServiceProxyApplication.class) |
||||
|
@SpringBootTest |
||||
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
|
public class UserControllerTest { |
||||
|
private MockMvc mockMvc; |
||||
|
|
||||
|
@Autowired |
||||
|
private WebApplicationContext wac; |
||||
|
|
||||
|
@Before |
||||
|
public void setUp() { |
||||
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldFetchAllUsers() throws Exception { |
||||
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/users/showAllUsers") |
||||
|
.accept(MediaType.APPLICATION_JSON)) |
||||
|
.andDo(print()) |
||||
|
.andExpect(jsonPath("$", hasSize(2))) |
||||
|
.andReturn(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldFindUserById() throws Exception { |
||||
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/users/findUserById/"+"101") |
||||
|
.accept(MediaType.APPLICATION_JSON)) |
||||
|
.andDo(print()) |
||||
|
.andExpect(jsonPath("$.userid").exists()) |
||||
|
.andExpect(jsonPath("$.userid").value(101)) |
||||
|
.andExpect(jsonPath("$.userfirstname").exists()) |
||||
|
.andExpect(jsonPath("$.userfirstname").value("Vignesh")) |
||||
|
.andExpect(jsonPath("$.useremail").exists()) |
||||
|
.andExpect(jsonPath("$.useremail").value("vignesh@gmail.com")) |
||||
|
.andExpect(jsonPath("$.*", hasSize(11))) |
||||
|
.andReturn(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldFindUserByName() throws Exception { |
||||
|
this.mockMvc.perform(MockMvcRequestBuilders.get("/users/findUserByName/"+"Vignesh") |
||||
|
.accept(MediaType.APPLICATION_JSON)) |
||||
|
.andDo(print()) |
||||
|
.andExpect(jsonPath("$.userid").exists()) |
||||
|
.andExpect(jsonPath("$.userid").value(101)) |
||||
|
.andExpect(jsonPath("$.userfirstname").exists()) |
||||
|
.andExpect(jsonPath("$.userfirstname").value("Vignesh")) |
||||
|
.andExpect(jsonPath("$.useremail").exists()) |
||||
|
.andExpect(jsonPath("$.useremail").value("vignesh@gmail.com")) |
||||
|
.andExpect(jsonPath("$.*", hasSize(11))) |
||||
|
.andReturn(); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue