Cucumber 测试未启动 Spring 启动应用程序
Cucumber test doesn't start Spring boot application
当开始我的 Cucumber 测试时(通过 Selenium 和 integration/rest),Spring 引导(测试)应用程序没有自动启动。
如何配置 Cucumber 以启动 Spring 引导应用程序(以及)?
我尝试了很多方法。我的文件是:
黄瓜主菜:
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/cucumber_integration_tests")
public class Cucumber_Integration_Test {
}
胶水代码文件类似于:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class StepDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8080/version");
}
etc.
}
在粘合代码文件中启动应用程序的替代方法:
@SpringBootTest( properties = "server.port=8080", classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class wine_Cucumber_Selenium_Steps {
private WebDriver driver;
private String baseUrl = "http://localhost";
private int port = 8080;
@Given("^I visit the wine cellar home page$")
public void goToWineCellarHomePage() {
// Firefox
// System.setProperty("webdriver.gecko.driver", "K:\k_schijf\download_via_firefox\geckodriver-v0.11.1-win64\geckodriver.exe");
// driver = new FirefoxDriver();
// Chrome
System.setProperty("webdriver.chrome.driver", "K:\k_schijf\download_via_firefox\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
baseUrl += ":" + port;
driver.navigate().to(baseUrl + "/index.html");
}
诊断:
- 我没有看到 Spring 以消息开头
- 我收到错误消息 "ResourceAccessException: I/O error on GET request for "http://localhost:8080/version":连接被拒绝"
您可能希望将@ContextConfiguration 与@SpringBootTest 注释一起使用。我有黄瓜测试工作示例 here
感谢 @ravinikam 指明了方向。我将进一步试验 ContextPath 和 SpringBootTest 的组合。
一个答案(也许不是最好的)是我使用了1.2.4版本的Cucumber并将这段代码添加到Cucumber的runTest中。那奏效了。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
public @interface CucumberStepsDefinition {
}
Bealdung 给出了另一个想法:请参阅 integration test 的这个简短示例。
当开始我的 Cucumber 测试时(通过 Selenium 和 integration/rest),Spring 引导(测试)应用程序没有自动启动。
如何配置 Cucumber 以启动 Spring 引导应用程序(以及)?
我尝试了很多方法。我的文件是:
黄瓜主菜:
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/cucumber_integration_tests")
public class Cucumber_Integration_Test {
}
胶水代码文件类似于:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class StepDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8080/version");
}
etc.
}
在粘合代码文件中启动应用程序的替代方法:
@SpringBootTest( properties = "server.port=8080", classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class wine_Cucumber_Selenium_Steps {
private WebDriver driver;
private String baseUrl = "http://localhost";
private int port = 8080;
@Given("^I visit the wine cellar home page$")
public void goToWineCellarHomePage() {
// Firefox
// System.setProperty("webdriver.gecko.driver", "K:\k_schijf\download_via_firefox\geckodriver-v0.11.1-win64\geckodriver.exe");
// driver = new FirefoxDriver();
// Chrome
System.setProperty("webdriver.chrome.driver", "K:\k_schijf\download_via_firefox\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
baseUrl += ":" + port;
driver.navigate().to(baseUrl + "/index.html");
}
诊断:
- 我没有看到 Spring 以消息开头
- 我收到错误消息 "ResourceAccessException: I/O error on GET request for "http://localhost:8080/version":连接被拒绝"
您可能希望将@ContextConfiguration 与@SpringBootTest 注释一起使用。我有黄瓜测试工作示例 here
感谢 @ravinikam 指明了方向。我将进一步试验 ContextPath 和 SpringBootTest 的组合。
一个答案(也许不是最好的)是我使用了1.2.4版本的Cucumber并将这段代码添加到Cucumber的runTest中。那奏效了。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(classes = Application.class, loader = SpringApplicationContextLoader.class)
@WebIntegrationTest
@RunWith(SpringJUnit4ClassRunner.class)
public @interface CucumberStepsDefinition {
}
Bealdung 给出了另一个想法:请参阅 integration test 的这个简短示例。