Kunaal Jambhore
Kunaal Jambhore

Reputation: 11

spring boot application test strategy advice

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.

Upvotes: 1

Views: 607

Answers (2)

Kunaal Jambhore
Kunaal Jambhore

Reputation: 11

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.

Upvotes: 0

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

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.

Upvotes: 1

Related Questions