0

In my project I have a service with the @Service annotation.

@Service
public class ExampleService { ... }

I would like to override this service with a Mockito-mock, using a configuration file for my test in the following way:

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

and in my test class:

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

For some reason the mocks aren't injected. The only way I can make this work is when I don't use @Service (@Component gives the same problem) but use a bean annotated method in the WebContext to load the ExampleService, and I put the TestClass behind the WebContext.class in the ContextConfiguration annotation (like in the code I wrote here). I don't get why, and I would like to know how I can keep using the @Service annotation.

raichu
  • 688
  • 2
  • 8
  • 15
  • looks like you are loading services from webcontext... which should have in another place in order to do your test configuration easy. You can use profiles in order to tell spring which bean wants lo load – Eddú Meléndez Mar 11 '15 at 15:48
  • WebContext imports another file Contextfile. Is it bad? Thanks for your idea, I'll look into it. – raichu Mar 12 '15 at 22:18

0 Answers0