Java 将命令行参数传递给以编程方式运行的 JUnit 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2884163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass command line arguments to JUnit test case being run programmatically
提问by Nick Vallely
I am attempting to run a JUnit Test from a Java Class with:
我正在尝试从 Java 类运行 JUnit 测试:
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.run(classToRun);
Problem is my JUnit test requires a database connection that is currently hardcoded in the JUnit test itself.
问题是我的 JUnit 测试需要一个当前在 JUnit 测试本身中硬编码的数据库连接。
What I am looking for is a way to run the JUnit test programmatically(above) but pass a database connection to it that I create in my Java Class that runs the test, and not hardcoded within the JUnit class.
我正在寻找的是一种以编程方式运行 JUnit 测试的方法(如上),但将我在运行测试的 Java 类中创建的数据库连接传递给它,而不是在 JUnit 类中进行硬编码。
Basically something like
基本上像
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.addParameters(java.sql.Connection);
core.run(classToRun);
Then within the classToRun:
然后在 classToRun 中:
@Test
Public void Test1(Connection dbConnection){
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select total from dual");
rs.next();
String myTotal = rs.getString("TOTAL");
//btw my tests are selenium testcases:)
selenium.isTextPresent(myTotal);
}
I know about The @Parameters, but it doesn't seem applicable here as it is more for running the same test case multiple times with differing values. I want all of my test cases to share a database connection that I pass in through a configuration file to my java client that then runs those test cases (also passed in through the configuration file).
我知道@Parameters,但它在这里似乎不适用,因为它更适合使用不同的值多次运行相同的测试用例。我希望我的所有测试用例共享我通过配置文件传递到我的 java 客户端的数据库连接,然后运行这些测试用例(也通过配置文件传入)。
Is this possible?
这可能吗?
P.S. I understand this seems like an odd way of doing things.
PS我知道这似乎是一种奇怪的做事方式。
采纳答案by tonio
You can use java system properties to achieve this.
您可以使用 java 系统属性来实现这一点。
Simply pass what you need with -Dconnectionstring=foobar
in the junit command line, or use the java api for system properties to set this programmatically, with System.setProperty(String name, String value)
, and System.getProperty(String name)
.
只需-Dconnectionstring=foobar
在 junit 命令行中传递您需要的内容,或者使用系统属性的 java api 以编程方式设置它,使用System.setProperty(String name, String value)
, 和System.getProperty(String name)
。
In your tests, you can use the @Before
or @BeforeClass
to set up common objects based on this property, pending on whether you want to run the setup once for each test (in which case you can use class members) or once for each suite (and then use static members).
在您的测试中,您可以使用@Before
or@BeforeClass
设置基于此属性的公共对象,取决于您是要为每个测试运行一次设置(在这种情况下您可以使用类成员)还是为每个套件运行一次(然后使用静态成员)。
You can even factorize this behavior by using an abstract class which all your test cases extends.
您甚至可以通过使用所有测试用例扩展的抽象类来分解此行为。