gpt4 book ai didi

java - 如何将正确的 Spring 上下文连接到我的 Cucumber 测试?

转载 作者:行者123 更新时间:2023-12-01 10:37:15 24 4
gpt4 key购买 nike

我的 Spring Web MVC 应用程序有一个有效的集成测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ShibaApplication.class)
@WebAppConfiguration
public class EchoControllerTests {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
private void setup() throws Exception {
this.mockMvc = webAppContextSetup(webApplicationContext).build();
}

@Test
public void echo() throws Exception {
mockMvc.perform(get("/echo/blargh"))
.andExpect(status().isOk())
.andExpect(content().string("blargh"));
}
}

保留(成功的)测试,我尝试创建一个相同的 Cucumber 测试。 cucumber 运行者是:

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/resources",
glue={"co.masslab.shiba", "cucumber.api.spring"})
public class CucumberTests {
}

定义 Cucumber 步骤的类如下所示:

@WebAppConfiguration
@Import(ShibaApplication.class)
@ContextConfiguration(classes=CucumberTests.class)
public class WebStepDefs {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;
private ResultActions resultActions;

@When("^the client calls the echo endpoint$")
public void the_client_calls() throws Exception {
Assert.notNull(webApplicationContext);
this.mockMvc = webAppContextSetup(webApplicationContext).build();
this.resultActions = mockMvc.perform(get("/echo/blargh"));
}

@Then("^the client receives a status code of 200$")
public void the_client_receives_a_status_code() throws Exception {
resultActions.andExpect(status().isOk());
}
}

但是, cucumber 测试失败,因为结果不是 200 而是 404。

我怀疑这是因为自动连接到 WebStepDefs 类中的 WebApplicationContext 与自动连接到 EchoControllerTests 中的 WebApplicationContext 不同。我一直在查看Spring JavaConfig Reference Guide v1.0.0.M4 ,但我还没有弄清楚我错在哪里。

最佳答案

我不断尝试不同的注释组合,终于找到了这个。对我有用的 WebStepsDef 注释是:

@ContextConfiguration(classes=ShibaApplication.class, loader=SpringApplicationContextLoader.class)
@IntegrationTest
@WebAppConfiguration

关于java - 如何将正确的 Spring 上下文连接到我的 Cucumber 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34598107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com