我正在尝试为我的弹簧应用程序执行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,它仅在前端和后端在同一端口(耦合在一起(上运行时才有效。
我使用了独立于弹簧框架的硒和铬驱动程序
这是一个基本的设置... .....
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驱动程序(旧版本似乎不起作用(