[英]How to create a MockMvc and a WebDriver in Spring 5
我編寫了以下簡單的測試用例來測試 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());
}
}
如果我執行它,我會得到 java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException 這是由 MockMvcHtmlUnitBuilder 在 before 方法中拋出的。 如果我刪除引發錯誤和驅動程序測試的行,則 mvcTest 不會成功,因為它得到 404 而不是 200。
所以接下來我做的是刪除 @WebAppConfiguration("/src/main/resources") 和 @ContextConfiguration(classes = {MvcConfig.class}) 注釋並添加 @SpringBootTest(classes = Application.class) 注釋。 現在 mvcTest 可以工作了,但是如果我再次為驅動程序添加代碼,它仍然會拋出 SessionNotFoundException。
所以我的問題是,如何在 Spring 5 中正確創建 MockMvc 和 WebDriver?
我找到了解決我的問題的方法。 它在 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());
}
}
使用當前的 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"));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.