Тесты - oyboy/Jora GitHub Wiki

Тест home-страницы

@WebMvcTest(HomeController.class)
public class HomeTest {
    @Autowired
    MockMvc mockMvc;
    @MockBean
    ProjectService projectService;
    @MockBean
    private UserService userService;
    @MockBean
    private UserRepository userRepository;
    @MockBean
    private ProjectRepository projectRepository;
    private Project project;
    private User user;
    @BeforeEach
    public void setup(){
       project = new Project();
        project.setId(1L);
        project.setTitle("Valid project");
        project.setDescription("Some description");
        project.setHash("hash123");

        when(projectRepository.findProjectByHash("hash123"))
                .thenReturn(project);
    }

    @Test
    @WithMockUser
    public void testStartPage() throws Exception{
        mockMvc.perform(get("/home"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("Мои проекты")))
                .andExpect(view().name("home"));
    }
    @Test
    @WithMockUser
    public void testNewProjectPage() throws Exception{
        mockMvc.perform(get("/home/create"))
                .andExpect(status().isOk())
                .andExpect(view().name("create-project"));
    }
    @Test
    @WithMockUser
    public void testCreatingProject_WhenValidInput_thenRedirectToTome() throws Exception{
        doNothing().when(projectService).saveProject(any(Project.class), any(User.class));

        mockMvc.perform(post("/home/create")
                        .param("title", project.getTitle())
                        .param("description", project.getDescription())
                        .with(csrf()))
                .andExpect(status().is3xxRedirection())
                .andExpect(redirectedUrl("/home"));

        verify(projectService, times(1)).saveProject(any(Project.class), eq(user));
    }
    @Test
    @WithMockUser
    public void testCreatingProject_WhenErrors_thenShowsCreateProjectPage() throws Exception{
        mockMvc.perform(post("/home/create")
                        .param("title", "")
                        .param("desctiption", "")
                        .with(csrf()))
                .andExpect(status().isOk())
                .andExpect(view().name("create-project"))
                .andExpect(model().attributeExists("errors"));
        verify(projectService, times(0)).saveProject(any(Project.class), eq(user));
    }
    @Test
    @WithMockUser
    public void tesJoinToProject_WhenUserAlreadyJoined() throws Exception{
        String projectHash = "hash123";
        doThrow(CustomException.UserAlreadyJoinedException.class)
                .when(projectService)
                .addUserToProject(projectHash, user);
        mockMvc.perform(post("/home/join")
                        .param("project_hash", projectHash)
                        .with(csrf()))
                .andExpect(status().isOk())
                .andExpect(view().name("home"))
                .andExpect(model().attributeExists("userError"));
    }
    @Test
    @WithMockUser
    public void testJoinToProject_WhenUserBanned() throws Exception {
        String projectHash = "hash123";
        doThrow(CustomException.UserBannedException.class)
                .when(projectService)
                .addUserToProject(projectHash, user);

        mockMvc.perform(post("/home/join")
                        .param("project_hash", projectHash)
                        .with(csrf()))
                .andExpect(status().isOk())
                .andExpect(view().name("home"))
                .andExpect(model().attributeExists("userError"));
    }
/*    @Test
    @WithMockUser
    public void testGetProjects() throws Exception {
        User testUser = new User();
        testUser.setId(1L);
        given(userService.getUserById(testUser.getId())).willReturn(testUser);

        List<Project> projects = Collections.singletonList(new Project());
        given(projectService.getProjectsForUser(testUser)).willReturn(projects);

        mockMvc.perform(get("/home"))
                .andExpect(status().isOk())
                .andExpect(model().attribute("projects", hasSize(1)));
    }*/
}
⚠️ **GitHub.com Fallback** ⚠️