0

在我的项目中,我有一个带有 @Service 注释的服务。

@Service
public class ExampleService { ... }

我想用 Mockito-mock 覆盖此服务,使用配置文件以下列方式进行我的测试:

public class TestContext{
   @Bean
   @Primary
   public ExampleService exampleService(){
      return mock(ExampleService.class);
   }
}

在我的测试课上:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { WebContext.class, TestContext.class})
@WebAppConfiguration
public class TestExample{...}

由于某种原因,没有注入模拟。我可以完成这项工作的唯一方法是当我不使用@Service(@Component 给出同样的问题)但在 WebContext 中使用 bean 注释方法来加载 ExampleService,并且我将 TestClass 放在 WebContext.class 的后面ContextConfiguration 注释(就像我在这里写的代码一样)。我不明白为什么,我想知道如何继续使用 @Service 注释。

4

0 回答 0