【问题标题】:How to create a MockMvc and a WebDriver in Spring 5如何在 Spring 5 中创建 MockMvc 和 WebDriver
【发布时间】:2018-03-28 06:42:48
【问题描述】:

我编写了以下简单的测试用例来测试 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?

【问题讨论】:

    标签: java spring spring-mvc spring-boot testing


    【解决方案1】:

    我找到了解决问题的方法。它在 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());
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用当前的 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"));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-08-19
        • 2017-06-03
        • 2018-07-28
        • 2013-10-03
        • 2019-11-02
        • 2018-04-25
        • 2016-02-05
        • 2016-03-13
        相关资源
        最近更新 更多