1
votes

I have a simple Spring Boot Application structure as follows

src/main/java
com
 +- example
     +- myproject
         +- Application.java
         |
         +- config
         |   +- SpringConfig.java
         |
         +- service
         |   +- DBService.java

src/main/test
com
 +- example
     +- myproject
         +- config
         |   +- MyTestRoot.java

src/test/resources
applicationContext-test.xml

Application.java is annotated with

@SpringBootApplication
@EnableJms
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration

SpringConfig.java is annotated with @Configuration and has a method which returns a new instance of DBService. The method is annotated with @Bean

@Bean
public DBService dbService() {
    return new DBService();
}

The DBService class has repositories Autowired into it. These repositories are from another project dependency and provide connection to RDBMS/Data Store.

I defined a bean in applicationContext-test.xml

<bean id="dbService" class="com.path.to.class.in.dependency"/>

When I autowire the dbService in my test class, I get an error "Error creating bean with name 'dbServices': Injection of autowired dependencies failed;"

What I am doing wrong here? I am using spring boot 1.3.5 and cannot use the @SpringBootTest annotation since it available from 1.4 onwards. Any help will be much appreciated.

EDIT: Since the DbService class in turn refers to repository classes (userRepositoty, customerRepository and so on), I tried to define the beans for those repositories too in the test context file. However, the repositories that I am injecting in the DbService class (in Main) are interfaces and the Spring Boot framework automatically resolves the actual implementations for me during runtime. I am not sure on how to do this in the test context.

2
@Maciej, Thanks for the answer but this is my current problem. My "DbService" in Main autowires repositories that are defined as interfaces. At runtime, Spring/Spring Boot provides me an implementation of those interfaces from my project dependencies automatically. I am trying to inject "DbService" in my test class "MyTestRoot" and I cannot figure how to tell spring to get a concrete implementation of the repositories while running a Unit Test. It throws an error : code Error creating bean with name 'dbServices': Injection of autowired dependencies failed codeKunaal Jambhore
Update question with your test class. I think you may have missed something there.Maciej Walkowiak

2 Answers

1
votes

Spring Boot does not import beans from XML files automatically. Use @ImportResource annotation on one of your configuration classes to create beans from XML file.

By the way, why do you create these beans in XML files? Just use @Configuration class for them too and keep things simple.

Additionally, when you use @SpringBootApplication you do not need:

  • @EnableAutoConfiguration
  • @EnableComponentScan

They are redundant. See @SpringBootApplication sources to find out what exactly annotations does it pull in.

0
votes

The following post I found worked for me: http://tuhrig.de/inject-mocks-with-springs-contextconfiguration/

My problem was I wanted the DbService in my Tests. But the DbService relied on other beans from external package dependencies that were resolved by spring at runtime. I setup a ServiceMockProvider class that returns a Mock of all the autowired beans that DbService uses. I created a base test class annotated with @ContextConfiguration and provided the DbService class and the ServiceMockProvider class in the classes attribute. All my test classes extend this base class and I'm good to go for testing.

Hope this is helpful for someone. And thanks to Thomas Uhrig for the post.