java在IntegrationTest服务器和测试之间共享Spring上下文
我正在尝试设置环境,以便通过方法调用配置对象,然后使用HTTP请求对其运行测试,因此(半伪代码):
myStore.addCustomer("Jim")
HttpResponse response = httpClient.get("http://localHost/customer/Jim")
assertThat(response.status, is(OK))
(或在JBehave中)
Given a customer named Jim
When I make an HTTP GET to path "customer/Jim"
Then the response is 200 OK
我想使用SpringBoot来实现web服务
然而,尽管我的尝试看起来很干净,但没有成功,因为我的测试对象看到的Spring上下文与web服务使用的Spring上下文不同
我的环境是Serenity+JBehave,但我希望这些原则与straight jUnit没有什么不同
我有:
@RunWith(SerenityRunner.class)
@SpringApplicationConfiguration(classes = Application.class )
@WebAppConfiguration
@IntegrationTest
public class AcceptanceTestSuite extends SerenityStories {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
private Store store;
}
。。。及申请代码:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
。。。以及我要共享的对象的类:
@Component
public class Store {
private String id;
private final Logger log = LoggerFactory.getLogger(Store.class);
public Store() {
log.info("Init");
}
public void addCustomer(String id) {
this.id = id;
log.info("Store " + this + " Set id " + id);
}
public String getCustomerId() {
log.info("Store " + this + " Return id " + id);
return id;
}
}
。。。在我的控制器中:
@RestController
public class LogNetController {
@Autowired Store store;
@RequestMapping("/customer/{name}")
public String read(name) {
return ...;
}
}
。。。在我的宁静步调课上:
@ContextConfiguration(classes = Application.class)
public class TestSteps extends ScenarioSteps {
@Autowired Store store;
@Step
public void addCustomer(String id) {
store.addCustomer(id);
}
}
当我运行测试时,服务器启动,setter运行,发出HTTP请求。然而
- 我可以看到
Store
构造函数记录了两次“Init”:一个由与测试关联的Spring上下文创建,另一个由属于Tomcat容器的Spring上下文创建李> - 我可以看到
Set
和Return
由Store
的不同实例记录。因此,我不get
值Iset
李>
如何让服务器和测试看到相同的Spring上下文
共 (0) 个答案