在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ Spring Boot Test中如何排除一些bean的注入?

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

問題

在測試Controller時,不希望測試方法走攔截器。經(jīng)過百度,使用的方法就是加入@ComponentScan的@excludeFilters 注解。但這行代碼只能在程序的主入口上起作用,如果加到Test的基類上是沒有任何反應的。我希望在測試時忽略Spring的自動注入并且不影響主程序的行為。謝謝

這是啟動程序的入口

/**
 * Spring could web程序主入口
 */
@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
@SpringBootApplication
public class JsGfrcServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(JsGfrcServiceApplication.class, args);
    }
}

這是我的測試基類


/**
 * Created by xx on 2018/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}))
@Transactional

public class MockMvcContractTest {

    static {
        System.setProperty("eureka.client.enabled", "false");
        System.setProperty("spring.cloud.config.failFast", "false");
        System.setProperty("spring.cloud.config.discovery.enabled", "false");
    }

    protected String packageGetParams(String paramName, List<String> values, String uri) {
        StringBuilder builder = new StringBuilder(uri);
        if (values == null || values.size() == 0) {
            return uri;
        }
        boolean contains = uri.contains("?");
        if (!contains) {
            builder.append("?");
        }
        for (String value : values) {
            builder.append(paramName).append("=").append(value).append("&");
        }
        builder.delete(builder.length() - 1, builder.length());
        return builder.toString();
    }

    @Autowired
    MockMvc mockMvc;

    @Before
    public void setup() {
        RestAssuredMockMvc.mockMvc(mockMvc);
    }

}
回答
編輯回答
巫婆

來自stackoverflow的答案

import org.springframework.boot.test.mock.mockito.MockBean;

public class SimpleTest {
    // 排除的filter
    @MockBean
    private Starter myTestBean;
    ...
}

Spring將使用這個mock而不是真正的類,所以不會調(diào)用@PostConstruct方法去創(chuàng)建。

2017年7月22日 05:24
編輯回答
夢囈

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = JsGfrcServiceApplication.class)

這classes 屬性不對,改成你的測試類

2017年5月28日 10:17