SpringSecurity 单元测试示例2

上一篇,SpringSecurity + MockMvc写单元测试(包括认证,授权,模拟Cookie)

测试示例如下

import lombok.SneakyThrows;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.Cookie;
import java.io.File;
import java.io.FileInputStream;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
@AutoConfigureMockMvc
public class UserControllerTest {
   

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() {
   
        this.mockMvc = MockMvcBuilders
                .webAppContextSetup(wac)
                .build();
    }

    /** * 测试失败,没有认证通过 * 当前用户不存在 * @throws Exception */
    @Test
    public void editEmail() throws Exception {
   
        mockMvc.perform(MockMvcRequestBuilders.get("/test/user/set-email?email=1121.ls.com")
                .contentType(MediaType.APPLICATION_JSON)
        )
                .andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    /** * 测试成功,认证通过 * @throws Exception */
    @Test
    @WithMockUser(username = "zx"/*, password = "11111111"*/)
    public void editEmail2() throws Exception {
   
        mockMvc.perform(MockMvcRequestBuilders.get("/test/user/set-email?email=1121.ls.com")
                .contentType(MediaType.APPLICATION_JSON)
        )
                .andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    /** * 测试成功,认证通过 * @throws Exception */
    @Test
    @WithUserDetails(value = "9032", userDetailsServiceBeanName = "userDetailsService")
    public void editEmail3() throws Exception {
   
        mockMvc.perform(MockMvcRequestBuilders.get("/test/user/set-email?email=1121.ls.com")
                .contentType(MediaType.APPLICATION_JSON)
        )
                .andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

    @Test
    public void editEmail4() throws Exception {
   
        mockMvc.perform(MockMvcRequestBuilders.get("/test/user/set-email?email=1121.ls.com")
                .header(LoginController.HEADER, "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6eCIsImV4cCI6MTU4MzQ3NzczOX0.3QYooiFT-dqc6k-TMNGD7Drhl1q5gvepYtjjpG05sHaZ_sucSWbxMApRvVCTF-aWg9hYJ6_omEr63YWYKoRdbw")
                .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
        )
                .andExpect(MockMvcResultMatchers.status().isOk()).andDo(print());
    }

}



全部评论

相关推荐

01-26 22:20
已编辑
门头沟学院 Java
Java抽象带篮子:项目很nb了,现在好好准备八股和算法吧,早点找实习,可以看看我的置顶帖子。帖子里写了怎么改简历,怎么包装实习经历,还有2个高质量可速成的项目话术,和我的牛客八股笔记专栏
点赞 评论 收藏
分享
02-08 20:56
已编辑
南京工业大学 Java
在等offer的比尔很洒脱:我也是在实习,项目先不说,感觉有点点小熟悉,但是我有点疑问,这第一个实习,公司真的让实习生去部署搭建和引入mq之类的吗,是不是有点过于信任了,我实习过的两个公司都是人家正式早搭好了,根本摸不到部署搭建的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务