我正在尝试为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中的OrderService就是null。我一定是错过了一些很明显的东西,但我想不出是什么。有什么建议吗?
发布于 2018-01-19 06:18:16
对我来说,这个异常也发生了。请看This Question。
尝试将@TestPropertySource(..)更改为
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
value={"spring.profiles.active=integrationtest"})希望能帮上忙!
https://stackoverflow.com/questions/48329375
复制相似问题