为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Boot Test中如何排除一些bean的注入?

Spring Boot Test中如何排除一些bean的注入?

红糖糍粑 2019-05-12 12:19:07
问题在测试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属性不对,改成你的测试类
                            
查看完整回答
反对 回复 2019-05-12
  • 2 回答
  • 0 关注
  • 3370 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信