首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring引导集成测试(业务层)

spring引导集成测试(业务层)
EN

Stack Overflow用户
提问于 2018-01-18 19:59:34
回答 1查看 835关注 0票数 0

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

代码语言:javascript
复制
// entities
@Entity
Table(name="TOrder")
public class JPAOrder... {
}

@Entity
Table(name="TCustomer")
public class JPACustomer... {
}
代码语言:javascript
复制
// Repository interfaces
@Repository
public interface OrderRepository extends CrudRepository<JPAOrder, Long> {
...
}

@Repository
public interface CustomerRepository extends CrudRepository<JPACustomer, Long> {
...
}
代码语言:javascript
复制
// Business logic
@Service
@Transactional
public class OrderService ... {
...
@Autowired
private CustomerRepository customerRepository;
...
public Order createOrderForCustomer(final Order order, final Customer customer) {
...
}
}
代码语言:javascript
复制
// 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(...);
}

启动应用程序会导致以下错误

代码语言:javascript
复制
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。我一定是错过了一些很明显的东西,但我想不出是什么。有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-19 06:18:16

对我来说,这个异常也发生了。请看This Question

尝试将@TestPropertySource(..)更改为

代码语言:javascript
复制
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
            value={"spring.profiles.active=integrationtest"})

希望能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48329375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档