问题描述
我正在尝试为Spring Boot应用程序的业务层设置集成测试.单元测试可以正常工作,但是集成测试不起作用.这是基本设置:
I'm trying to setup integration tests for the business layer of a spring boot application. The Unit-Tests work fine but integration tests don't work. Here's the basic setup:
// entities
@Entity
Table(name="TOrder")
public class JPAOrder... {
}
@Entity
Table(name="TCustomer")
public class JPACustomer... {
}
// Repository interfaces
@Repository
public interface OrderRepository extends CrudRepository<JPAOrder, Long> {
...
}
@Repository
public interface CustomerRepository extends CrudRepository<JPACustomer, Long> {
...
}
// Business logic
@Service
@Transactional
public class OrderService ... {
...
@Autowired
private CustomerRepository customerRepository;
...
public Order createOrderForCustomer(final Order order, final Customer customer) {
...
}
}
// Test
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
public class OrderIntegrationTest {
@SpyBean
private OrderRepository orderRepository;
@Autowired
private OrderService orderService;
Order order = orderService.createOrderForCustomer(...);
}
启动应用程序会出现此错误
Starting the application gives me this error
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.我一定会错过一些确实很明显的东西,但我不知道该怎么办.有什么建议吗?
If I don't use the @SpyBean annotation in the integration test the orderRepository in the OrderService is simply null.I must be missing something really obvious but I can't figure out what.Any suggestions?
推荐答案
对我来说,也发生了此异常.请参阅此问题.
For me also this exception occured. Please see This Question.
尝试更改@TestPropertySource(..)
到
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
value={"spring.profiles.active=integrationtest"})
希望有帮助!
这篇关于Spring Boot集成测试(业务层)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!