Spring boot 这里有什么区别-@Autowired和@MockBean

Spring boot 这里有什么区别-@Autowired和@MockBean,spring-boot,junit,mockito,spring-test,spring-boot-test,Spring Boot,Junit,Mockito,Spring Test,Spring Boot Test,我在SpringBoot项目中为我们的服务类编写单元测试。当我自动连接正在测试的类时,测试正常运行,当我使用@Autowire的@MockBean insead时,测试失败 @SpringBootTest class SignupServiceTest { @Autowired SignupService signupService; @MockBean DSService dsService; @MockBean SignupHelper signupHelper; @

我在SpringBoot项目中为我们的服务类编写单元测试。当我自动连接正在测试的类时,测试正常运行,当我使用@Autowire的@MockBean insead时,测试失败

@SpringBootTest
class SignupServiceTest {

  @Autowired SignupService signupService;

  @MockBean DSService dsService;

  @MockBean SignupHelper signupHelper;

  @MockBean SessionHelper sessionHelper;

  @MockBean CommonService commonService;

有人能帮我解决这个问题吗?@MockBean失败的原因。还有一种方法可以在mockito中模拟autowired类(当前类)的方法。

a@SpringBootTest是一种实际启动Spring容器和应用程序bean的测试

@测试中的自动连线字段的行为与普通SpringBean中的相同;它们被注入应用程序中配置的实际Bean(xml、@Configuration中的@Bean或@Component/@Service)

@MockBean创建一个模拟,它们的行为类似于普通模拟,您可以使用
when/then
等进行控制,并使用
verify
等进行检查。它们的特殊之处在于它们被注入到上下文中的其他bean中(例如,当您调用
Mockito.initAnnotations(this)