我们可以在空手道功能中使用Spring bean吗? [英] Can we access spring bean in Karate feature?
问题描述
我有一个如下所示的类,可以在空手道功能文件中访问myServer对象或调用handleOperation()方法(可以使用注入的bean)吗?如果是,那我能知道吗?
I have a class like below, can I access the myServer object or call handleOperation() method (which can use the injected bean) in Karate Feature file? If yes then may I know how?
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApiApp.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = {AcceptanceTestConfiguration.class})
@ActiveProfiles("test")
@KarateOptions(features = "classpath:acceptanceTest/api/myapi.feature", tags = "@myapi")
public class MyAtddTest {
@Autowired
private MyServer myServer;
public void handleOperation() throws Exception {
myServer.handle();
}
}
推荐答案
不直接支持spring或注释.而且不确定是否可以混合使用测试注释.
There is no direct support for spring or the annotations. And not sure if you can mix the test annotations.
但是在这里看看Spring MVC Dispatcher示例: https://github.com/intuit/karate/tree/master/karate-mock-servlet#mocking-your-servlet
But take a look at the Spring MVC Dispatcher example here: https://github.com/intuit/karate/tree/master/karate-mock-servlet#mocking-your-servlet
具体说来,如何使用Java interop可以完成任何您想做的事情.我建议使用第一原理获得spring上下文.例如:
Specifically how using Java interop you can do anything you want. I recommend getting the spring context using first-principles. For e.g:
ApplicationContext context = new AnnotationConfigApplicationContext(AcceptanceTestConfiguration.class);
然后从中取出豆类.通过System.setProperty()
设置测试配置文件应该很简单,搜索它.您甚至可以在karate-config.js
中完成所有这些操作,然后在所有Scenario
-s中都应该易于使用.
And then getting beans out of it. Setting a test profile via System.setProperty()
should be simple, search for it. You can do all this in even the karate-config.js
and then it should be easy to use from all Scenario
-s.
这篇关于我们可以在空手道功能中使用Spring bean吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!