`
cqh520llr
  • 浏览: 480627 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

springboot测试

 
阅读更多
package com.macro.mall;

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.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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.web.context.WebApplicationContext;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;

@WebAppConfiguration
//@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest(classes = Application.class)
public class PmsDaoTests {

    @Test
    public void contextLoads() {
    }

    @Autowired
    private WebApplicationContext wac;

    //@Autowired
    private MockMvc mockMvc;

    //@Autowired
    //private SecurityContext context;

    /**
     * 在每次测试执行前构建mvc环境
     */
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        Collection objects = new ArrayList<>();
        objects.add(new SimpleGrantedAuthority("Test"));
        SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("user", new AdminUserDetails(new UmsAdmin(), null), objects));
        //context.setAuthentication(new AnonymousAuthenticationToken("user", new Object(), null));
    }

    /**
     * 测试上传文件
     */
    @Test
    public void whenUploadFileSuccess() {
        try {
            File file = new File("C:\\xx真情\\xxx.jpg");
            //文件之外的参数
            String key = "/2019/12/18/" + file.getName();
            String result = mockMvc.perform(
                    MockMvcRequestBuilders
                            .fileUpload("/picture/upload")
                            .file(new MockMultipartFile("file",
                                    "test.txt",
                                    MediaType.MULTIPART_FORM_DATA_VALUE, new FileInputStream(file)))
                            .param("key", key))//参数
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andReturn().getResponse().getContentAsString();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics