I'm writing a Spring Boot (1.5.7) REST application, which is working nice without problems.
Now I'm trying to write some unit/integration tests but I can't figure how to configure them.
This is my Application.java
@EntityScan
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
@Bean
public Module hibernate5Module()
{
return new Hibernate5Module();
}
}
This is one of my test, NetworkServiceTest.java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles(profiles = "test")
@DataJpaTest
public class NetworkServiceTest
{
@Autowired
private TestEntityManager manager;
@Autowired
private NetworkService service;
@Test
public void whenFindByFacilityIdAndSportId_thenReturnNetwork()
{
// Given
Network net = new Network(100, 200);
manager.persist(net);
manager.flush();
// When
Network found = service.findByFacilityIdAndSportId(net.getFacilityId(), net.getSportId());
// Then
assertThat(found.getFacilityId()).isEqualTo(net.getFacilityId());
assertThat(found.getSportId()).isEqualTo(net.getSportId());
}
}
All tests return "NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.NetworkService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"
This is the configuration file application-test.yml
spring:
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
liquibase:
enabled: false