Browse Source

added few testcases for user controller

master
Gowrisankar J g 4 years ago
parent
commit
b52eb5082a
4 changed files with 85 additions and 10 deletions
  1. 6
      UB_UserServiceProxy/pom.xml
  2. 4
      UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/controller/UserController.java
  3. 8
      UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/model/User.java
  4. 77
      UB_UserServiceProxy/src/test/java/com/example/urbanbazaar/controller/UserControllerTest.java

6
UB_UserServiceProxy/pom.xml

@ -42,6 +42,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

4
UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/controller/UserController.java

@ -15,14 +15,12 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.function.EntityResponse;
import com.example.urbanbazaar.model.User;
import com.example.urbanbazaar.model.UserLogin;
import com.example.urbanbazaar.repository.UserRepository;
//@CrossOrigin(origins = "http://localhost:4405")
@CrossOrigin(origins = "http://localhost:63871")
//@CrossOrigin(origins = "http://localhost:8000")
@RestController
@Service

8
UB_UserServiceProxy/src/main/java/com/example/urbanbazaar/model/User.java

@ -1,24 +1,18 @@
package com.example.urbanbazaar.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.GenerationType;
//daouser
@Entity
@Table(name = "users")
public class User {
private int userid;
private String useremail;
private String userpassword;
private String userfirstname;
private String userlastname;
private String usercity;

77
UB_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();
}
}
Loading…
Cancel
Save