spring开机集成测试(业务层)

spring boot integration test (business layer)

我正在尝试为 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"})

希望对您有所帮助!