1

I have MyTestClass1:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class MyTestClass1 {

    @Configuration
    static class Config {
        @Bean
        public FileSerivce fileService() {
            return Mockito.mock(FileSerivce.class);
        }
    }
}

and the other test class MyTestClass2:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTestClass2 {

    @Autowired
    FileSerivce fileService;

}

Problem is, that in class MyTestClass2 fileService is mock autowired from MyTestClass1 instead of real fileService. When I remove mock definition from MyTestClass1, MyTestClass2 uses real fileService as i need.

How can i remove MyTestClass1 side effect on MyTestClass2 ?

dulo
  • 145
  • 2
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/10553815/autowiring-beans-implementing-same-interface-how-to-autowire-a-particular-depe – Nilesh Jul 11 '14 at 08:58

2 Answers2

1

You can add data to the @ContextConfiguration annotation, for instance a specific Configuration to load. Alternatively, you can add the @Qualifier annotation to the FileService in TestClass2 and force an instance of FileService with a given name to be used.

So for instance, if your real FileService bean were called "fileServiceImpl", in MyTestClass2, you could have

@Autowired
@Qualifier("fileServiceImpl")
FileService fileService;
Ben Green
  • 3,953
  • 3
  • 29
  • 49
  • Thanks for answer @Ben Green. The option with Qualifier wouldnt be helpfull for me because in TestClass2 i am using also another Services that autowire FileService, so i would have to put Qualifier also to every Service that autowire FileService, but than i think it would be little more complicated in MyTestClass1 to replace real bean with mock – dulo Jul 11 '14 at 12:00
  • Actaully I think dont understand why one TestClass has impact on another TestClass even when i use DirtiesContext annotation. If there is way, have to completely clean context after MyTestClass1 (with FileService mock), it would be crucial for me. – dulo Jul 11 '14 at 12:05
  • What about my first comment then of specifying the Configuration to use? If you make the inner class private and then in the MyTestClass1 change the `@ContextConfiguration` annotation to `@ContextConfiguration(classes={Config.class})` – Ben Green Jul 11 '14 at 12:07
0

To the bean definition that is expected to mock FileSerivce, add the name identifier to @Bean: @Bean(name="mockfileService") or similar.

Then, when you will only need to add the @Qualifier annotation to the classes using the mock version of the bean:

@Autowired
@Qualifier("mockfileService")
FileService fileService;

Basically, if you are going to have two different implementations for FileService (the mock and the real service) you need to add identifiers so Springs know which one to use.

Or you could use a different context configuration for your mocks, but this would not be the best choice.

pfernandom
  • 929
  • 10
  • 22