弹簧启动集成测试(业务层)



我正在尝试为 Spring 启动应用程序的业务层设置集成测试。单元测试工作正常,但集成测试不起作用。以下是基本设置:

// 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(...);
}

启动应用程序给我这个错误

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 就是空。我一定错过了一些非常明显的东西,但我无法弄清楚是什么。有什么建议吗?

对我来说,也发生了这种异常。请看这个问题。

尝试更改@TestPropertySource(..)

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
            value={"spring.profiles.active=integrationtest"})

希望对您有所帮助!

最新更新