Reputation: 1244
I have a Spring Boot 2 application with the following structure:
- src/main/java/com.mycompany/
---- application
---- domain
---- infrastructure
-------- persistence
------------ ...
-------- Application.java
- src/main/test/java/com.mycompany/
---- application
---- domain
---- infrastructure
-------- persistence
------------ testingutils
---------------- JdbcPersistenceHelper.java
------------ CurrenciesJdbcViewTest.java
Application class:
package com.mycompany.infrastructure;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
JdbcPersistenceHelper:
package com.mycompany.infrastructure.persistence.testingutils;
@Component
public class JdbcPersistenceHelper {
private EntityManager entityManager;
@Autowired
public JdbcPersistenceHelper(EntityManager entityManager) {
this.entityManager = entityManager;
}
CurrenciesJdbcViewTest:
package com.mycompany.infrastructure.persistence;
@DataJpaTest
public class CurrenciesJdbcViewTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private JdbcPersistenceHelper persistenceHelper;
private CurrenciesJdbcView view;
@BeforeEach
void setUp() {
view = new CurrenciesJdbcView(jdbcTemplate);
}
However, when I run the test, I got an error on ApplicationContext loading as follows:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.mycompany.infrastructure.persistence.CurrenciesJdbcViewTest': Unsatisfied dependency expressed through field 'persistenceHelper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mycompany.infrastructure.persistence.testingutils.JdbcPersistenceHelper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
It seems that spring is not detecting and autowiring JdbcPersistenceHelper
class even though it is placed in a subpackage of com.mycompany.infrastructure
where my spring boot's application class is located, so I think it should be able to detect it without any further configuration.
Am I missing something here?
Upvotes: 0
Views: 1195
Reputation: 77226
You're only using @DataJpaTest
on your test class; most likely, your "helper" (which I recommend replacing with Spring Data JPA) isn't in that slice and needs to be added to it using includeFilters
or @ContextConfiguration
.
Upvotes: 2
Reputation: 16209
You have 'src/main/test/com.mycompany/' but that should be 'src/test/java/com/mycompany/'
I guess 'test' is seen as a package name and therefore not picked up by component scan.
If you want to use dependency injection for your tests, you might want to consider constructor injection in favor of field injection since it is considered better style and clearer (all required dependencies must be set).
I think I would not use D.I. at all for my tests but just instantiate any helpers as fields or in a @Before method. I don't really see a test as an application component but more as a standalone thing. Less complexity helps with understanding and maintaing tests in my experience.
Upvotes: 3