SpringMVC 4.3 nacos1.4 mock2.0 junit4.13 测试Controller

发布于:2024-05-19 ⋅ 阅读:(168) ⋅ 点赞:(0)

使用@WebAppConfiguration @ContextConfiguration 注解的方式报错:

nacos 和spring 注解加载前后有问题,换了思路解决问题

Caused by: java.lang.IllegalStateException: org.springframework.context.support.GenericApplicationContext@40ef3420 has not been refreshed yet
	at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1069)
	at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1183)
	at com.alibaba.spring.util.BeanUtils.getBeanNames(BeanUtils.java:153)
	at com.alibaba.spring.util.BeanUtils.getBeanNames(BeanUtils.java:136)
	at com.alibaba.nacos.spring.util.NacosBeanUtils.isBeanDefinitionPresent(NacosBeanUtils.java:207)
	at com.alibaba.nacos.spring.util.NacosBeanUtils.registerInfrastructureBeanIfAbsent(NacosBeanUtils.java:174)
	at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosApplicationContextHolder(NacosBeanUtils.java:285)
	at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosCommonBeans(NacosBeanUtils.java:372)
	at com.alibaba.nacos.spring.context.annotation.NacosBeanDefinitionRegistrar.registerNacosAnnotationBeans(NacosBeanDefinitionRegistrar.java:83)
	at com.alibaba.nacos.spring.context.config.xml.NacosAnnotationDrivenBeanDefinitionParser.parse(NacosAnnotationDrivenBeanDefinitionParser.java:49)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1435)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1419)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:172)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)

解决思路:使用XmlWebApplicationContext 创建Spring的rootWebApplicationContext

再使用XmlWebApplicationContext创建Spring的webApplicationContext,两者绑定下,在给到

MockMvcBuilders,测试就完成了MockMvc初始化,就可以使用mockMvc.perform(requestBuilder)请求你的Controller的地址了

private MockMvc mockMvc;
	XmlWebApplicationContext webApplicationContext;
    @Before
    public void setup() {
    	
      // 创建web应用程序根上下文,该上下文解析并管理系统实例  
      XmlWebApplicationContext rootWebApplicationContext = new XmlWebApplicationContext();  
      rootWebApplicationContext.setConfigLocations("classpath:spring-context.xml");  
      rootWebApplicationContext.refresh(); 
      // 创建servletContext上下文  
      // 创建web应用程序上下文,管理controller层请求业务  
      webApplicationContext = new XmlWebApplicationContext();  
      webApplicationContext.setConfigLocations("classpath:spring-mvc.xml","classpath:spring-mvc-rest.xml","classpath:spring-mvc-modeler.xml");  
      webApplicationContext.setParent(rootWebApplicationContext);  
      //servletContext.get
      MockServletContext servletContext = new MockServletContext();  
      webApplicationContext.setServletContext(servletContext);  
      webApplicationContext.refresh();  
      Map<String, Object> mm =  webApplicationContext.getBeansWithAnnotation(Controller.class);
      servletContext.getServletContextName();
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);  
      mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      
    }
	
	@Test
	public void testGet() {
		MockHttpServletRequestBuilder requestBuilder =
				MockMvcRequestBuilders.post("/longtp/offlinebak/get")
                .param("id", "1");
		
		try {
            MockHttpServletRequest request = mockMvc.perform(requestBuilder)
            .andDo(MockMvcResultHandlers.print()) // 打印请求、响应及其他相关信息
            .andExpect(MockMvcResultMatchers.status().isOk()) // 期待返回的状态是OK【200】
            //.andExpect(MockMvcResultMatchers.content().string("id")) // 期待返回的内容是“hello”
           // .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_VALUE)) // 预期返回的媒体类型是JSON
            //.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/result.jsp")) // 预期请求到此页面
            .andReturn()
            .getRequest();
            // 拿到了请求对象。。。
        } catch (Exception e) {
            e.printStackTrace();
        }

	}


网站公告

今日签到

点亮在社区的每一天
去签到