问题在测试Controller时,不希望测试方法走拦截器。经过百度,使用的方法就是加入@ComponentScan的@excludeFilters注解。但这行代码只能在程序的主入口上起作用,如果加到Test的基类上是没有任何反应的。我希望在测试时忽略Spring的自动注入并且不影响主程序的行为。谢谢这是启动程序的入口/***Springcouldweb程序主入口*/@Configuration//配置控制@EnableAutoConfiguration//启用自动配置@EnableFeignClients(basePackages={"com.konyo.service.client","com.konyo.activiticommon.client","com.konyo.teleport.common.inteceptor"})@MapperScan(value={"com.konyo.service.dao","com.konyo.activiticommon.mapper"})@ComponentScans(value={@ComponentScan(value={"com.konyo.teleport","com.konyo.service","com.konyo.activiticommon"})//组件扫描})@EnableDiscoveryClient@EnableEurekaClient@SpringBootApplicationpublicclassJsGfrcServiceApplication{publicstaticvoidmain(String[]args){SpringApplication.run(JsGfrcServiceApplication.class,args);}}这是我的测试基类/***Createdbyxxon2018/07/12.*Contract测试基类*/@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.MOCK,classes=JsGfrcServiceApplication.class)@AutoConfigureMockMvc@AutoConfigureJsonTesters@DirtiesContext//我希望启动时忽略这些类,但是这行代码不起作用@ComponentScan(value={"com.konyo.service","com.konyo.activiticommon"},excludeFilters=@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,classes={RedisConfig.class,SwaggerConfig.class,TxManagerHttpRequestServiceImpl.class,TxManagerTxUrlServiceImpl.class,InitDictionaryMap.class}))@TransactionalpublicclassMockMvcContractTest{static{System.setProperty("eureka.client.enabled","false");System.setProperty("spring.cloud.config.failFast","false");System.setProperty("spring.cloud.config.discovery.enabled","false");}protectedStringpackageGetParams(StringparamName,Listvalues,Stringuri){StringBuilderbuilder=newStringBuilder(uri);if(values==null||values.size()==0){returnuri;}booleancontains=uri.contains("?");if(!contains){builder.append("?");}for(Stringvalue:values){builder.append(paramName).append("=").append(value).append("&");}builder.delete(builder.length()-1,builder.length());returnbuilder.toString();}@AutowiredMockMvcmockMvc;@Beforepublicvoidsetup(){RestAssuredMockMvc.mockMvc(mockMvc);}}
2 回答
慕容708150
TA贡献1831条经验 获得超4个赞
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.MOCK,classes=JsGfrcServiceApplication.class)这classes属性不对,改成你的测试类
添加回答
举报
0/150
提交
取消