master
parent
0a94406f82
commit
a21dd29e01
@ -0,0 +1,12 @@ |
||||
package spring; |
||||
|
||||
import org.springframework.context.annotation.ComponentScan; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
||||
@Configuration |
||||
@ComponentScan({"web.controller","web.service"}) |
||||
public class SpringConfig implements WebMvcConfigurer { |
||||
|
||||
|
||||
} |
@ -0,0 +1,50 @@ |
||||
package spring; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
import org.springframework.test.web.servlet.MockMvc; |
||||
import org.springframework.test.web.servlet.ResultActions; |
||||
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; |
||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; |
||||
import web.controller.MenuController; |
||||
|
||||
//让测试运行于Spring环境
|
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@SpringJUnitWebConfig |
||||
@ContextConfiguration(classes = SpringConfig.class) |
||||
public class UrlTest { |
||||
|
||||
private MockMvc mockMvc; |
||||
|
||||
private Logger log = LogManager.getLogger(); |
||||
|
||||
@Autowired |
||||
private WebApplicationContext wac; |
||||
|
||||
@Before |
||||
public void setup() { |
||||
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void buildUrl() { |
||||
log.info(MvcUriComponentsBuilder.fromController(MenuController.class).build()); |
||||
} |
||||
|
||||
@Test |
||||
public void testJson() throws Exception { |
||||
ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.get("/test/task")); |
||||
resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); |
||||
} |
||||
} |
Loading…
Reference in new issue