springMock的使用

  1. mockmvc使用示例
  2. shiro测试

mockmvc使用示例

import com.App;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.event.annotation.BeforeTestMethod;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

@RunWith(SpringRunner.class)
//App.class 启动类
@SpringBootTest(classes = {App.class})
public class TestMock {
	@Autowired
	WebApplicationContext wac;

	private MockMvc mvc;

    /**
    * 任何测试方法执行前执行
    */
	@Before
	public void prepareMockMvc() {
		this.mvc = webAppContextSetup(wac).build();
	}


/**
    * 任何测试方法执行后执行
    */
	@After
	public void endMockMvc() {

	}

//MockMvcRequestBuilders.multipart().file( MockMultipartFile  mockMultipartFile)  测试文件上传

	@Test
	public void test() throws  Exception{
		MvcResult res= mvc.perform(MockMvcRequestBuilders.get("/hello"))
				.andExpect(ResultMatcher.matchAll(status().isOk()))
				.andExpect(ResultMatcher.matchAll(content().contentType(MediaType.APPLICATION_JSON)))
				.andDo(MockMvcResultHandlers.log()).andReturn();
        System.out.print(res.getResponce().getContextAsString());//获取返回结果
	}

    /**
    * 忽略test1
    */
    @Ignore
    @Test
	public void test1() throws  Exception{
		MvcResult res= mvc.perform(MockMvcRequestBuilders.get("/hello").param("a","1"))
				.andExpect(ResultMatcher.matchAll(status().isOk()))
				.andExpect(ResultMatcher.matchAll(content().contentType(MediaType.APPLICATION_JSON)))
				.andDo(MockMvcResultHandlers.log()).andReturn();
	}
    
/**  @beforeClass  @AfterClass  所以测试方法前执行 所以方法执行后执行  必须使用 public static void  修饰方法 
     执行顺序 1.@beforeClass 2.@Before 3.@Test 4.@After 5.@AfterClass
*/

}

shiro测试


private MockMvc mvc;

@Autowired
private WebApplicationContext context;

@Resource
private SecurityManager securityManager;
private Subject subject;
private MockHttpServletRequest mockHttpServletRequest;
private MockHttpServletResponce mockHttpServletResponce;

@Before
public void setup(){
mockHttpServletRequest = new MockHttpServletRequest(context.getServletContext());
mockHttpServletResponce = new MockHttpServletResponce();

MockHttpSession mockHttpSession = new MockHttpSession(context.getServletContext())
mockHttpServletRequest.setSession(mockHttpSession);
SecurityUtils.setSecurityManager(securityManager);
this.mvc = webAppContextSetup(wac).build();

subject = new WebSubject.builder(mockHttpServletRequest,mockHttpServletResponce).buildWebSubject();

UsernamePasswordToken  token = new UsernamePasswordToken(......);
subject.login(token);
ThreadContext.bind(subject);
}


@Test
public void Test(){
}