Java 在IntegrationTest服务器和测试之间共享Spring上下文
我正在尝试设置环境,以便通过方法调用配置对象,然后使用HTTP请求对其运行测试,因此(半伪代码): (或在JBehave中) 我想使用SpringBoot来实现web服务 然而,尽管我的尝试看起来很干净,但没有成功,因为我的测试对象看到的Spring上下文与web服务使用的Spring上下文不同Java 在IntegrationTest服务器和测试之间共享Spring上下文,java,spring-boot,serenity-bdd,Java,Spring Boot,Serenity Bdd,我正在尝试设置环境,以便通过方法调用配置对象,然后使用HTTP请求对其运行测试,因此(半伪代码): (或在JBehave中) 我想使用SpringBoot来实现web服务 然而,尽管我的尝试看起来很干净,但没有成功,因为我的测试对象看到的Spring上下文与web服务使用的Spring上下文不同 我的环境是Serenity+JBehave,但我希望这些原则与straight jUnit没有什么不同 我有: @RunWith(SerenityRunner.class) @SpringApplic
我的环境是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请求。然而
- 我可以看到
构造函数记录了两次“Init”:一个由与测试相关联的Spring上下文创建,另一个由属于Tomcat容器的Spring上下文创建Store
- 我可以看到
和Set
由Return
的不同实例记录。因此,我不Store
获取
设置的值
@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);
}
}