Files
love-nav/src/test/java/com/lovenav/CommentTest.java
2023-12-20 20:48:17 +08:00

49 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.lovenav;
import com.lovenav.controller.CommentController;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
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.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
/**
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
@MapperScan("com/lovenav/dao")
public class CommentTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {
//此种方式可通过spring上下文来自动配置一个或多个controller
//mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
//此种方式手工指定想要的controller
mockMvc = MockMvcBuilders.standaloneSetup(new CommentController()).build();
}
@Test
public void Test() throws Exception{
//构造请求
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/comment/hello");
// MockMvcResultHandlers.print();
mockMvc.perform(request);
}
}