0
當我將應用程序作爲Spring Boot應用程序啓動時,ServiceEndpointConfig會正確自動裝配。但是,當我作爲Junit測試運行時,出現以下異常。我正在使用application.yml文件和不同的配置文件。彈簧單元測試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyServiceContextConfig.class,
loader = SpringApplicationContextLoader.class)
@ActiveProfiles({"unit", "statsd-none"})
public class MyServiceTest
{
}
@Configuration
public class MyServiceContextConfig {
@Bean
public MyService myServiceImpl(){
return new MyServiceImpl();
}
}
@Configuration
@Component
@EnableConfigurationProperties
@ComponentScan("com.myservice")
@Import({ServiceEndpointConfig.class})
public class MyServiceImpl implements MyService {
@Autowired
ServiceEndpointConfig serviceEndpointConfig;
}
@Configuration
@Component
@ConfigurationProperties(prefix="service")
public class ServiceEndpointConfig
{
}
錯誤:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myServiceImpl':
Unsatisfied dependency expressed through field 'serviceEndpointConfig': No qualifying bean of type [com.myservice.config.ServiceEndpointConfig] found