Created
July 30, 2020 06:35
-
-
Save iamvickyav/f10b9a2fe456f2cdad25f53501948aab to your computer and use it in GitHub Desktop.
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 org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.boot.test.mock.mockito.MockBean; | |
import org.springframework.security.test.context.support.WithMockUser; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
import org.springframework.web.context.WebApplicationContext; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import static org.hamcrest.Matchers.is; | |
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | |
import static org.mockito.Mockito.when; | |
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
@AutoConfigureMockMvc | |
public class TodoControllerTest { | |
@Autowired | |
private MockMvc mockMvc; | |
@Autowired | |
private WebApplicationContext context; | |
@MockBean | |
private UserService userService; | |
@BeforeEach | |
public void setup() { | |
mockMvc = MockMvcBuilders | |
.webAppContextSetup(context) | |
.apply(springSecurity()) | |
.build(); | |
} | |
@WithMockUser | |
@Test | |
void test() throws Exception { | |
List<User> users = Arrays.asList( | |
new User(1, "Vicky", new Date(), "Chennai")); | |
when(userService.getAllUser()).thenReturn(users); | |
mockMvc.perform(get("/user")) | |
// .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) | |
.andExpect(status().isOk()) | |
.andExpect(jsonPath("$", hasSize(1))) | |
.andExpect(jsonPath("$[0].name", is("Vicky"))) | |
.andExpect(jsonPath("$[0].city", is("Chennai"))); | |
} | |
@WithMockUser | |
@Test | |
void test2() throws Exception { | |
User user = new User(1, "Vicky", new Date(), "Chennai"); | |
when(userService.getUser("Vicky")).thenReturn(user); | |
mockMvc.perform(get("/user/"+user.getName())) | |
// .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) | |
.andExpect(status().isOk()) | |
.andExpect(jsonPath("$.name", is("Vicky"))) | |
.andExpect(jsonPath("$.city", is("Chennai"))); | |
} | |
} | |
// In pom.xml | |
/** | |
<dependency> | |
<groupId>org.springframework.security</groupId> | |
<artifactId>spring-security-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment