![](/img/trans.png)
[英]When running Swing application with the Spring Framework, Unit tests do not work
[英]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框架的Selenium和chrome驱动程序
这是一个基本设置……..
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.