在尝试使用spring-boot-starter-test测试多线程应用程序之后:
APIConnector apiConnector1 = (APIConnector) applicationContext.getBean("test");
APIConnector apiConnector2 = (APIConnector) applicationContext.getBean("test");
APIConnector apiConnector3 = (APIConnector) applicationContext.getBean("test");
这是测试bean代码:
@Component("test")
@Scope("prototype")
public class Test {}
APIConnector的第一个实例被模拟, 但所有其他新bean都是真实的类实例。
@Profile("test")
@Configuration
public class APIConnectorTestConfiguration {
@Bean
@Primary
public APIConnector apiConnectorTest() {
APIConnector mock = mock(APIConnector.class);
when(mock.test(anyString(), anyDouble(), anyDouble()))
.thenReturn(true);
}