Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Give suites access to test objects #189

Open
drekka opened this issue Feb 4, 2011 · 8 comments
Open

Enhancement: Give suites access to test objects #189

drekka opened this issue Feb 4, 2011 · 8 comments
Labels

Comments

@drekka
Copy link

drekka commented Feb 4, 2011

I'm currently working with a legacy test suite which has a very costly setup and teardown. It's old code which uses an extension of TestSetup to start and stop the server. It overrides setUp() to start the server and then loop through each class in the suite and inject references to server objects into them.

I'm trying to work out how to do this using the new @suite and @Suite.Class annotations. I've searched the web and read a lot of stuff on @rule and MethodRules. I cannot see how to do it. There doesn't appear to be any way that stuff setup in a @BeforeClass in a @suite annotated class can be passed/injected into the tests being run.

Another thought was to create an extension of Suite and override one or more methods. But I could see how to get information from the @BeforeClass method of the class annotated with @suite into each suite class after it is instantiated.

@matthewfarwell
Copy link
Contributor

One simple option would be to use a Rule (specifically ExternalResource), which accesses gets stuff from a public static method in the Suite. This would set up the server if it's not already done. Then, in the AfterClass in the Suite, you'd do the teardown. For more information, see my answer to How can I run different junit test based on the same (but changing) information/setup (http://stackoverflow.com/questions/8007284/how-can-i-run-different-junit-test-based-on-the-same-but-changing-information/8007983#8007983) on Stackoverflow.

@Tibor17
Copy link
Contributor

Tibor17 commented Jan 4, 2012

Hi guys, i think you are talking about the feature i proposed in #308.
I implemented on my side, so i will try to provide the solution in my public repository.

Is it what you are looking for?

T

@Tibor17
Copy link
Contributor

Tibor17 commented Jun 28, 2013

@dsaff
Has the JUnit added any feature so far that the Suite's reference would be injected into suite's children?
Maybe not necessarily the suite's ref itself, perhaps whatever object the suite has created before running children.
Thx, Tibor

1 similar comment
@Tibor17
Copy link
Contributor

Tibor17 commented Jun 28, 2013

@dsaff
Has the JUnit added any feature so far that the Suite's reference would be injected into suite's children?
Maybe not necessarily the suite's ref itself, perhaps whatever object the suite has created before running children.
Thx, Tibor

@dsaff
Copy link
Member

dsaff commented Jun 28, 2013

@Tibor17, not in general. I think this is a place to move very carefully, because if the same mutable object gets injected into multiple child tests, there's the danger that test B fails due to a mutation made by test A.

I almost believe that it's better to encourage people who want to use what is essentially static state to do so using static state, since most Java developers have good intuition about how that works.

@Tibor17
Copy link
Contributor

Tibor17 commented Jul 20, 2013

@dsaff
I can clearly see that the beauty of this feature is that you do not have any ugly singleton to share in your app.
It's the same as it is in other frameworks with @Inject annotation. Do not call singleton, we will call you. Easy!

Regarding the mutability, we are not going to inject runner instance nor the test-class instance. The injected obj is just pure custom obj. Other frameworks with IoC do not need to care if it's mutable or not. I do not want to imaging how if it is generic type or java.lang.Object.

The custom obj then should be provided into child via constructor in ParentRunner or by adding setter method there.

If we decide to implement such feature and then you @dsaff are idle as usually when you do not like some pressure from the community and discard the pull request, then it will waste our time on both sides.
Do not you think so?
Hm?

@dsaff
Copy link
Member

dsaff commented Jul 23, 2013

@Tibor17,

Did you post a link to your public repository with a solution like you mentioned above?

@Tibor17
Copy link
Contributor

Tibor17 commented Jul 25, 2013

@dsaff
Yes, I will provide it in my public repo. Give me few days, now I am busy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants