在Cucumber step实现中,可以通过以下步骤来@Autowire一个REST服务类(Spring Boot):
@RestController
注解标记该类,并使用@RequestMapping
注解指定REST接口的路径。@Autowired
注解将需要的依赖注入进来。例如:@RestController
@RequestMapping("/api")
public class MyRestController {
@Autowired
private MyService myService;
@GetMapping("/data")
public String getData() {
return myService.getData();
}
}
@Autowired
注解将REST服务类注入进来。确保Cucumber的step实现类也被Spring容器管理。例如:@RunWith(Cucumber.class)
@ContextConfiguration(classes = {MyRestController.class, MyStepDefinitions.class})
public class MyStepDefinitions {
@Autowired
private MyRestController myRestController;
// Step definitions...
}
cucumber.xml
文件中添加以下配置:<cucumber-jvm-parallel-plugin>
<threadsCount>1</threadsCount>
<featuresDirectory>src/test/resources/features</featuresDirectory>
<glue>com.example.steps</glue>
<plugins>
<plugin>
<name>json</name>
</plugin>
</plugins>
</cucumber-jvm-parallel-plugin>
myRestController
来调用REST服务类中的方法了。例如:@Given("I have a REST service")
public void iHaveARestService() {
// Call REST service method
String data = myRestController.getData();
// Assertion or further steps...
}
这样,你就可以在Cucumber step实现中成功@Autowire一个REST服务类了。
关于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体的云计算品牌商,建议你参考腾讯云官方文档或咨询腾讯云的技术支持团队,获取与你的需求相匹配的产品和服务信息。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云