我编写了以下简单的测试用例来测试MockMvc和WebDriver:
@RunWith(SpringRunner.class)
@WebAppConfiguration("/src/main/resources")
@ContextConfiguration(classes = {MvcConfig.class})
public class exampleTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
private WebDriver driver;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(this.context).build();
}
@Test
public void mvcTest() throws Exception {
mvc.perform(get("/")).andExpect(status().isOk());
}
@Test
public void driverTest() {
this.driver.get("http://localhost:8080/");
assertEquals("Please log in", this.driver.findElement(By.xpath("/html/body/form/h1")).getText());
}
}如果我执行它,我会得到由MockMvcHtmlUnitBuilder在之前的方法中抛出的java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException。如果我删除抛出错误和驱动程序测试的行,mvcTest不会成功,因为它得到的是404而不是200。
因此,我做的下一件事是删除@WebAppConfiguration("/src/main/resources")和@ContextConfiguration(classes = {MvcConfig.class})注释,并添加@SpringBootTest(classes = Application.class)注释。现在mvcTest可以工作了,但是如果我再次添加驱动程序的代码,它仍然会抛出SessionNotFoundException。
所以我的问题是,如何在Spring5中正确地创建MockMvc和WebDriver?
发布于 2018-03-30 18:03:21
我找到了解决我的问题的办法。它在Spring文档中提到您必须安装org.seleniumhq.selenium:selenium-htmlunit-driver依赖项。最新版本是2.52.0。我现在所做的是添加相同版本的远程驱动程序:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.52.0</version>
</dependency>我还使用了@SpringBootTest注释,所以我的最终测试类锁定如下:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class exampleTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
private WebClient webClient;
private WebDriver driver;
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
this.webClient = MockMvcWebClientBuilder.webAppContextSetup(context, springSecurity()).build();
this.driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(this.context, springSecurity()).build();
}
@Test
public void mvcTest() throws Exception {
mvc.perform(get("/login"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Please log in")));
}
@Test
public void clientTest() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
HtmlPage loginPage = webClient.getPage("http://localhost:8080/app/");
List<DomElement> pageList = loginPage.getElementsByTagName("h1");
DomElement page = pageList.get(0);
String text = page.getTextContent();
assertThat(text).isEqualTo("Please log in");
}
@Test
public void driverTest() {
driver.get("http://localhost:8080/app/");
assertEquals("Please log in",driver.findElement(By.xpath("/html/body/form/h1")).getText());
}
}发布于 2020-06-26 19:58:57
使用当前的Spring Boot和Spring Boot Test版本,您只需执行以下操作:
@SpringBootTest
@AutoConfigureMockMvc
public class MyApplicationTests {
@Autowired
private WebApplicationContext context;
@Autowired
private MockMvc mockMvc;
@Autowired
private WebDriver driver;
@Test
void contextLoads() {
}
@Test
public void shouldReturnDefaultMessage() throws Exception {
this.driver.get("http://localhost:8080/app/");
assertEquals("Please log in",driver.findElement(By.xpath("/html/body/form/h1")).getText());
or
this.mockMvc.perform(get("/app")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string("true"));
}
}https://stackoverflow.com/questions/49527760
复制相似问题