Похоже, вы столкнулись с ошибкой с огурцом. Cucumber не может найти конфигурацию контекста в ваших определениях шагов, поэтому он возвращается к GenericApplicationContext
, который не может быть обновлен между сценариями ios.
Попробуйте добавить конфигурацию контекста к вашему шагу определения. Например:
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── Application.java
└── test
├── java
│ └── com
│ └── example
│ ├── RunCucumberTest.java
│ └── StepDefinitions.java
└── resources
└── com
└── example
└── example.feature
package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
}
package com.example;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"})
public class RunCucumberTest {
}
package com.example;
import io.cucumber.java.en.Given;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import static org.junit.Assert.assertNotNull;
@SpringBootTest
public class StepDefinitions {
@Autowired
private ApplicationContext applicationContext;
@Given("an application context")
public void anApplicationContext() {
assertNotNull(applicationContext);
}
}
Поскольку StepDefinitions
аннотирован с помощью конфигурации контекста (@SpringBootTest
), огурец передаст это в Springs TestContextManager
. Это создаст контекст приложения и убедится, что выполнение сценария похоже на тест JUnit с SpringRunner/SpringExtension
.
. Здесь также можно использовать другие конфигурации контекста. Например:
package com.example;
import io.cucumber.java.en.Given;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.assertNotNull;
@ContextConfiguration("classpath:cucumber.xml")
public class StepDefinitions {
@Autowired
private ApplicationContext applicationContext;
@Given("an application context")
public void anApplicationContext() {
assertNotNull(applicationContext);
}
}