【发布时间】:2018-11-17 14:49:49
【问题描述】:
让我们考虑一个非常简单的例子来说明我的观点:
@SpringBootTest
class Tmp extends Specification{
@Autowired
private CarService carService;
def "getCarById"(int id) {
return carService != null ? carService.getById(id) : new Car();
}
def "validate number of doors"(Car car, int expectedNrOfDoors) {
expect:
car.getNrOfDoors() == expectedNrOfDoors
where:
car || expectedNrOfDoors
getCarById(1) || 3
getCarById(2) || 3
getCarById(3) || 5
}
}
将调用第一个getCarById(_) 方法。然后会创建上下文,然后执行validate number of doors test。
是否可以“在一开始”创建上下文?为了在getCarById(_) 方法中访问它(和carService)?
【问题讨论】:
-
将该代码放在构造函数 Tmp() 中。
-
我不关注。你能详细说明一下吗?
标签: spring-boot integration-testing spock