spring boot集成测试(业务层)
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。我肯定是漏掉了一些非常明显的东西,但我无法弄清楚是什么。有什么建议吗?