English 中文(简体)
Passing Object Parameters for TestNG methods?
原标题:

Here is my situation.

Before my tests are run, in the beforesuite, I instantiate a bunch of "environment objects" These objects get created based on my environment configuration file. It is my tests that will actually be using these environment objects. The problem is how can I pass them to the tests.

Your first impulse might be to use a data provider, which will pass the correct objects to the test. The problem is that the data provider will have to know how to refer to the correct objects and then pass them. As it stands now the environment objects can only be referred to by name (string -> object)

My other option is to pass the test method a string which refers to the correct objects. But this has the same problem as above.

Both of these options create lots of dependencies and problems. I also have to make lots of assumptions about the type the key refers to.

Most of this problem stems from the fact that I abusing TestNG. What i really need is my own controller that can instantiate my Test object while passing the appropriate environment objects.

I was looking into testng s TestRunner interface, but I m not sure if this would work.

Any and all help appreciated. Eric

最佳回答

Why not use a mix of @Factory and @DataProvider?

The factory will be in charge of creating your test instances and passing them the correct parameters: either strings (in which case the data provider can then turn these strings into objects) or directly the objects, in which case your tests can use these values directly.

Feel free to post some code if this doesn t help you solve your problem.

-- Cedric

问题回答

I had a similar situation and ended up using java properties instead.

You may use the property in your code like this and pass in the variables to your JVM with the -Dmyproperty.value=hello. I used it for stuff like environment path, service url, timeout amount, etc.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签