Can we access spring bean in Karate feature?

烂漫一生 提交于 2019-12-24 11:22:50

问题


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();
    }
}

回答1:


There is no direct support for spring or the annotations. And not sure if you can mix the test annotations.

But take a look at the Spring MVC Dispatcher example here: https://github.com/intuit/karate/tree/master/karate-mock-servlet#mocking-your-servlet

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);

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.



来源:https://stackoverflow.com/questions/54572485/can-we-access-spring-bean-in-karate-feature

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!