从API在常规应用程序上下文而不是测试上下文中运行测试

问题描述

我想创建一个黄瓜测试应用程序,该应用程序将从POST- @RequestBody之类的API输入中获取功能文件,然后对该文件运行测试。根据我的研究,除了在@SpringBoottest中设置所有内容,将测试文件包含在资源中然后运行mvn test之外,我看不到任何其他可行的方法。这不是运行测试的好方法,因为您必须进入应用程序以添加修改方案文件,然后运行测试。对于其他人来说,从带有文件输入的API运行测试会容易得多,因为Cucumber的目标是使它变得人性化,而无需进行大量编码。

解决方法

我发现此解决方案有效。虽然,它没有为您提供外壳。

  • 使用测试单元创建普通应用程序。
  • 确保已安装maven / gradle并将其添加到PATH
  • 在常规应用程序内,创建使用以下代码的方法:

// windows: "cmd","/c"
// unix: "/bin/sh","-c"
ProcessBuilder builder = new ProcessBuilder("cmd","/c","mvn","test");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
// code to process test result from reader
// I only need to send this back to as API response. So there were no other impl