Бегущие огуречно-пышные черты на фоне пружинного сапога
Я пытался заставить огурцов работать с пружинными сапогами, но все шло не так. Я получаю ошибку org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8080/applicants": Connection refused; nested exception is java.net.ConnectException: Connection refused
который, кажется, указывает на то, что он достигает конечной точки, но служба не работает.
Я читал, что мне нужно иметь cucumber.xml
файл, но мой проект не использует конфигурацию xml, это все аннотации, так что вместо этого я получил это:
package support
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.base.package")
public class CucumberConfiguration {}
Я добавил это к World
, но это, кажется, неправильный способ делать вещи (то есть я не знаю, как добавить аннотацию на groovy step defs).
package support
import com.thing.app.Application
import org.junit.runner.RunWith
import org.springframework.boot.test.SpringApplicationContextLoader
import org.springframework.boot.test.WebIntegrationTest
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration
import static cucumber.api.groovy.Hooks.*
//@RunWith(SpringJUnit4ClassRunner)
//@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
//@WebAppConfiguration
//@WebIntegrationTest
@ContextConfiguration(classes = CucumberConfiguration)
public class AbstractTest {
}
World() {
new AbstractTest()
}
Before() {}
After() {}
Я оставил в своих других аннотациях, чтобы показать, что я сделал до сих пор. Ничего из этого не сработало.
Я также попытался настроить класс AbstractDefs, как показано здесь https://github.com/jakehschwartz/spring-boot-cucumber-example/tree/master/src/test/java/demo, но это также не сработало главным образом потому, что я не использую стиль вещей cucumber-java, а вместо этого стиль cucumber-groovy, который не использует классы определения шагов.
Редактировать: Просто обнаружил, что я делал что-то неправильно, имея env.groovy
Я привык к рубиновому огурцу, поэтому у меня проблемы с поиском всех маленьких проблем. Тем не менее у меня все та же проблема, я не знаю, как выполнить в контексте Spring.
0 ответов
Вы можете создать экземпляр контекста теста Spring с помощью io.cucumber.spring.SpringFactory
и зарегистрируйте адаптер в World
чтобы разрешить Groovy script иметь доступ к Spring beans:
env.groovy:
@ContextConfiguration(classes = TestConfiguration, loader = SpringBootContextLoader)
class CucumberContextConfiguration {
}
//adapter bypassing World properties to Spring context
class SpringFactoryWorldAdapter {
private final SpringFactory factory;
SpringFactoryWorldAdapter(SpringFactory factory) {
this.factory = factory;
}
@Override
Object getProperty(String s) {
return factory.testContextManager.getContext().getBean(s);
}
}
def factory; //Keep state to prevent repeated context initialization
World { args ->
if (factory == null) {
factory = new SpringFactory()
factory.addClass(CucumberContextConfiguration)
factory.start()
}
new SpringFactoryWorldAdapter(factory)
}