簡體   English   中英

使用另一個在不同端口上運行的框架(vuejs)在前端運行時如何對spring應用程序執行BDD測試

[英]How do I perform BDD test for spring application when am running the the front end using another framework (vuejs) which is running on different port

我正在嘗試為我的spring應用程序執行BDD測試。 我正在將vuejs用於在不同端口上運行的前端。 我的問題是spring應用程序無法連接到前端應用程序(返回404狀態)

這是有關如何連接到前端的代碼。

import static org.assertj.core.api.Assertions.assertThat;
@ContextConfiguration(classes = MainApplication.class)
@WebAppConfiguration
public class PlantRequestSteps {

List<Plant> plants = new ArrayList<>();

@Autowired
private WebApplicationContext wac;

private WebClient customerBrowser;
HtmlPage customerPage;

@Autowired
PlantHireRepository plantHireRepository;


@Before
public void setUp() {

    customerBrowser = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
}

@After
public void tearOff() {
    plantHireRepository.deleteAll();
    plants.clear();
}

@Given("^the following plants are vailable from this given date$")
public void the_following_plants_are_vailable_from_this_given_date(DataTable table){
    for (Map<String, String> row: table.asMaps(String.class, String.class)){
        plants.add(Plant.of(
                Long.parseLong(row.get("id")), row.get("name"),
                row.get("description"), new BigDecimal(row.get("price")),
                LocalDate.parse(row.get("date"))));
    }
}

@Given("^am on BuildIT's  \"([^\"]*)\" web page$")
public void am_on_BuildIT_s_web_page(String arg1) throws Throwable {
    customerPage = customerBrowser.getPage("http://localhost:8081/");
}
}

我設法通過更改測試庫解決了我的問題。 以前我使用的是HtmlUnit ,它僅在前端和后端都在同一端口(耦合在一起)上運行時才起作用。

我使用了獨立於Spring框架的Seleniumchrome驅動程序

這是一個基本設置……..

public class PlantRequestSteps  {

@Autowired
private WebApplicationContext wac;

private WebDriver driver ;;


static {
 // you should specify the path where you installed your chrome driver
// as the second parameter to this function 
  System.setProperty("webdriver.chrome.driver", "/path/chromedriver");
}


@Before
public void setup() {

    driver = new ChromeDriver();

}

@After
public void tearoff() {

   driver.close();

}

@Given("^ that am on this   \"([^\"]*)\" web page$")
public void that_am_on_this_web_page(String arg1) throws Throwable {
    driver.get("http://localhost:8081/");
}

也不要忘記在Junit庫之外添加庫。 我正在使用maveen,所以我在pom.xml文件中添加了我的。

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.11.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.9.1</version>
        <scope>test</scope>
    </dependency>

最后確保您安裝了最新版本的chrome驅動程序(舊版本似乎無法正常工作)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM