当前位置 : 主页 > 编程语言 > java >

mock测试应用

来源:互联网 收集:自由互联 发布时间:2021-06-28
ApplicationTests.java package com.suneee.wxsk;import com.suneee.wxsk.domain.User;import com.suneee.wxsk.repository.UserRepository;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Auto
ApplicationTests.java
package com.suneee.wxsk;

import com.suneee.wxsk.domain.User;
import com.suneee.wxsk.repository.UserRepository;
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.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTests {
	@Autowired
	private MockMvc mockMvc;
	@Autowired
	private UserRepository userRepository;
	@Test
	public void contextLoads() throws Exception {
		List
 
   users = userRepository.findDistinctByNameOrEmail("wxsk", "wxsk@163.com");
		System.out.println(users.toString());
		/*断言测试*/
		assertEquals(userRepository.findByName("wxsk").getName(),"wxsk");
		assertEquals(userRepository.findByEmail("wxsk@163.com").getName(),"温馨时刻");
		/*mockMvc模拟测试url请求*/
		mockMvc.perform(MockMvcRequestBuilders.get("/hello")
				.accept(MediaType.APPLICATION_JSON))
				.andExpect(status().isOk()).andExpect(content().string(equalTo("Hello World ...")));
	}

}
 
网友评论