spring boot集成测试(业务层)

6 浏览
0 Comments

spring boot集成测试(业务层)

我正在尝试为Spring Boot应用程序的业务层设置集成测试。单元测试运行正常,但集成测试无法运行。以下是基本设置:

// 实体类

@Entity

Table(name="TOrder")

public class JPAOrder... {

}

@Entity

Table(name="TCustomer")

public class JPACustomer... {

}

// 仓库接口

@Repository

public interface OrderRepository extends CrudRepository {

...

}

@Repository

public interface CustomerRepository extends CrudRepository {

...

}

// 业务逻辑

@Service

@Transactional

public class OrderService ... {

...

@Autowired

private CustomerRepository customerRepository;

...

public Order createOrderForCustomer(final Order order, final Customer customer) {

...

}

}

// 测试

@RunWith(SpringRunner.class)

@TestPropertySource(locations = "classpath:application-integrationtest.properties")

public class OrderIntegrationTest {

@SpyBean

private OrderRepository orderRepository;

@Autowired

private OrderService orderService;

Order order = orderService.createOrderForCustomer(...);

}

启动应用程序时出现以下错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '...repository.OrderRepository#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [...repository.OrderRepository]: Specified class is an interface

...

如果在集成测试中不使用@SpyBean注解,则OrderService中的orderRepository将为null。我肯定是漏掉了一些非常明显的东西,但我无法弄清楚是什么。有什么建议吗?

0
0 Comments

在进行Spring Boot集成测试(业务层)时,出现了一个问题。原因可能是在使用@TestPropertySource注解时,配置的属性源无法正常工作。可以通过修改注解中的参数来解决这个问题,具体的解决方法是将参数改为(webEnvironment = WebEnvironment.RANDOM_PORT, value={"spring.profiles.active=integrationtest"})。这样做可能会导致web环境被加载,这是无法避免的。

0