无法在Controller构造函数中模拟注入的依赖关系

时间:2017-05-31 13:27:05

标签: spring unit-testing spring-boot dependency-injection

我在模拟注入控制器的对象时遇到了问题。

我正在进行集成测试。在我的测试中,我有以下设置:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AuthenticationTests {

@TestConfiguration
public class Config {
    @Bean
    @Primary
    public AbstractClient client() {
        return new AbstractClient() {

            @Override
            public ManagedChannel getChannel() {
                return new ManagedChannel();
            }
        };
    }
    }
}

在正在测试的控制器中,AbstractClient是依赖注入的,如下所示:

@Controller
public class MyController {
    private ManagedChannel managedChannel;
    public MyController(AbstractClient client) {
      managedChannel = client.getChannel();
    }
} 

每当我运行测试时,@TestConfiguration类中定义的AbstractClient都不会被注入 - 而是默认注释(注释为@Service)。

有人可以帮忙吗?

0 个答案:

没有答案
相关问题