如何通过mvn命令顺序执行2个Java类 [英] How to sequentially execute 2 Java classes via mvn command

查看:222
本文介绍了如何通过mvn命令顺序执行2个Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有共生关系的Java类。

I have 2 Java classes which have a symbiotic relationship.

Class 1产生一些输出文件,Class 2使用class 1的输出并验证它。这两个类都从命令行获取输入。这个项目是以maven为基础的。

Class 1 produces some output files and Class 2 consumes the output of class 1 and validates it. Both of these classes take input from the commandline. This project is maven based.

鉴于这种共生特性,我不确定如何连接它们?

Given this symbiotic nature, I am unsure how to "connect them"?

我的想法是,编写另一个Java类,它接受命令行输入并调用2个类。但是这里还有另一个不确定因素,我怎么能运行第1类(为了生成输出文件),所以我可以使用第2类来验证它。也许是Junit @Before或者一些注释?我真的不确定如何继续。我希望我在这里有意义。

My thinking was, to write another Java class which takes in command line inputs and calls the 2 classes. However there is another uncertainty here, how could I run class 1 (in order to produce the output files) so then I can have class 2 to validate it. Perhaps Junit @Before or some annotation? I really am unsure how to proceed. I hope I am making sense here.

我们非常感谢任何帮助或建议。

Any help or suggestions would be much appreciated.

推荐答案

执行您所测试类的 main()方法在JUnit方法中。

Execute the main() method of your class under test from within a JUnit method.

public class Class2 {
  @Before
  public void produceOutputFiles() {
      Class1.main(new String[] { "these", "are", "commandline", "arguments"});
  }

  @Test
  public void validateClass1Output() {
      //read in the files and validate the output
  }
}

通过 Process.exec()调用Class1是一个有许多缺点的选项。将测试代码和测试代码保持在同一个JVM中要简单得多。

Invoking Class1 via Process.exec() is an option with many downsides. It is much simpler to keep both the test and the tested code within the same JVM.

这篇关于如何通过mvn命令顺序执行2个Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆