首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Spring5中创建MockMvc和WebDriver

如何在Spring5中创建MockMvc和WebDriver
EN

Stack Overflow用户
提问于 2018-03-28 14:42:49
回答 2查看 687关注 0票数 0

我编写了以下简单的测试用例来测试MockMvc和WebDriver:

代码语言:javascript
复制
@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?

EN

回答 2

Stack Overflow用户

发布于 2018-03-30 18:03:21

我找到了解决我的问题的办法。它在Spring文档中提到您必须安装org.seleniumhq.selenium:selenium-htmlunit-driver依赖项。最新版本是2.52.0。我现在所做的是添加相同版本的远程驱动程序:

代码语言:javascript
复制
    <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注释,所以我的最终测试类锁定如下:

代码语言:javascript
复制
@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());
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-26 19:58:57

使用当前的Spring Boot和Spring Boot Test版本,您只需执行以下操作:

代码语言:javascript
复制
@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"));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49527760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档