在Spring Boot中运行测试在上下文中失败

时间:2017-09-28 13:56:40

标签: spring-boot spring-boot-test

我正在编写一个Spring Boot(1.5.7)REST应用程序,它运行良好而没有任何问题。

现在我正在尝试编写一些单元/集成测试,但我无法确定如何配置它们。

这是我的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();
    }
}

这是我的测试之一,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());
    }
}

所有测试返回" NoSuchBeanDefinitionException:没有符合条件的类型' com.example.NetworkService'可用:预计至少有1个豆有资格作为autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}"

这是配置文件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

3 个答案:

答案 0 :(得分:2)

解决了来自@ darren-forsythe

的提示

BaseTest.java

@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "test")
abstract class BaseTest
{
    @Test
    public void init() {}
}

BaseDatabaseTest.java

@DataJpaTest
@ComponentScan(basePackages = { "com.example.services"})
abstract class BaseDatabaseTest extends BaseTest
{
    @Autowired
    protected TestEntityManager m_pManager;

    @Test
    public void initDatabase() {}
}

NetworkServiceTest.java

public class NetworkServiceTest extends BaseDatabaseTest
{
    @Autowired
    private NetworkService service;

    @Test
    public void whenFindByFacilityIdAndSportId_thenReturnNetwork()
    {
        long facilityId = 100;
        long sportId = 200;

        // Given
        Network net = new Network(facilityId, sportId);
        m_pManager.persist(net);
        m_pManager.flush();

        assertThat(net.getFacilityId()).isEqualTo(facilityId);

        // When
        List<Network> nets = service.findAll();
        Network found = service.findByFacilityIdAndSportId(facilityId, sportId);

        // Then
        assertThat(nets.size()).isGreaterThan(0);
        assertThat(found.getFacilityId()).isEqualTo(net.getFacilityId());
        assertThat(found.getSportId()).isEqualTo(net.getSportId());
        assertThat(found.getSportId()).isNotEqualTo(0);
    }
}

答案 1 :(得分:1)

您已使用@DataJpaTest注释了测试类,但您似乎正在测试服务层(因为您尝试自动装配NetworkService),而不是JPA层。

尝试删除@DataJpaTest注释。

答案 2 :(得分:0)

我会调查Mockito。我发现它对于单元测试弹簧启动应用非常有用。

你最终会有类似的东西。

public class NetworkServiceTest
{
    @Mock
    private TestEntityManager manager;
    @Mock
    private NetworkService service;

    ....
}