Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.car.rental.project;
- import com.car.rental.project.model.User;
- import com.car.rental.project.service.UserService;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.hamcrest.Matchers;
- import org.junit.Before;
- import org.junit.jupiter.api.Test;
- import org.junit.runner.RunWith;
- import org.mockito.Mockito;
- 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.test.context.junit4.SpringRunner;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.result.ModelResultMatchers;
- import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
- import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- @AutoConfigureMockMvc
- class UserControllerTest {
- @Autowired
- private MockMvc mockMvc;
- @Autowired
- private UserService userService;
- @Before
- public void setUp() {
- User user = new User();
- Mockito.when(userService.findByUsername(user.getUsername()))
- .thenReturn(user);
- }
- @Test
- void souldRegisterNewUser() throws Exception{
- User u = new User();
- u.setUsername("user432");
- u.setPassword("user123123");
- u.setPasswordConfirm("user123123");
- this.mockMvc.perform(post("/registration")
- .param("username",u.getUsername())
- .param("password",u.getPassword())
- .param("passwordConfirm",u.getPasswordConfirm()))
- .andExpect(status().isFound())
- .andExpect(redirectedUrl("/index"))
- .andExpect(flash().attribute("register", Matchers.equalTo("true")));
- }
- @Test
- void souldNotRegisterNewUserBecousePasswordIsBad() throws Exception{
- User u = new User();
- u.setUsername("user432");
- u.setPassword("user1231231");
- u.setPasswordConfirm("user123123");
- this.mockMvc.perform(post("/registration")
- .param("username",u.getUsername())
- .param("password",u.getPassword())
- .param("passwordConfirm",u.getPasswordConfirm()))
- .andExpect(status().isFound())
- .andExpect(redirectedUrl("/index"))
- .andExpect(flash().attribute("error",Matchers.equalTo("password")));
- }
- @Test
- void indexShouldReturnOk() throws Exception {
- this.mockMvc.perform(get("/")).andExpect(status().isOk());
- }
- @Test
- void flotaShouldReturnOk() throws Exception {
- this.mockMvc.perform(get("/flota").with(user("user").roles("USER"))).andExpect(status().isOk());
- }
- @Test
- void adminPanelShouldReturnForbidden() throws Exception {
- this.mockMvc.perform(get("/adminPanel").with(user("user").roles("USER"))).andExpect(status().isForbidden());
- }
- @Test
- void flotaShouldRedirect() throws Exception {
- this.mockMvc.perform(get("/flota"))
- .andExpect(status().is(302))
- .andExpect(redirectedUrl("http://localhost/login"));
- }
- }
Add Comment
Please, Sign In to add comment