andrewaylett / atunit

Run your unit tests with automatic DI and Mocking
Apache License 2.0
1 stars 0 forks source link

Provide access to the AtUnit Spring context from the unit test #8

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use AtUnit in a Spring 2 unit test using the approach proposed in <a
href="http://arsenalist.com/2007/06/18/unit-testing-struts-2-actions-spring-juni
t/">Arenalist</a>
2. Try to merge the Spring context used by Struts (which contains the
actions) with the Spring context used by AtUnit (which contains the mocks)
3. No solution found! This same problem may arise in other scenarios other
than a Struts 2 unit test.

What is the expected output? What do you see instead?
There's no easy way of merging both contexts.

What version of the product are you using? On what operating system?
AtUnit 1.0

Please provide any additional information below.

I've implemented a solution for this (with good results) using a new AtUnit
Annotation that marks a field so atunit.spring.SpringContainer can inject
the AtUnit Spring context there. This way it's easy to merge both contexts.

I've also implemented the unit tests, so if you think this is a good design
you could give me write access to the repository so I can update it with
the changes or send me an email.

Original issue reported on code.google.com by migueldi...@gmail.com on 9 Jan 2008 at 5:23

GoogleCodeExporter commented 8 years ago
Interesting.  Yeah, this is a gap on the Spring side that doesn't exist on the 
Guice
side, and it should definitely be closed.

I'd very much like to see your solution.  Can you attach a patch and maybe a 
sample
test so I can see what you've done?

Original comment by logan.jo...@gmail.com on 9 Jan 2008 at 5:35

GoogleCodeExporter commented 8 years ago
This is the code that makes it: 
 - a small modification to SpringContainer
 - the new ContextInstance annotation
 - a new Exception: TooManyContextInstancesException (only 1
ContextInstance-annotated field should exist, maybe it's not necessary...)

I don't have a simple and complete test sample available right now (I use 
several
base test classes)
but I provide a modification of the mentioned Arenalist base test class for 
Struts 2
(StrutsTestBase.java).

Original comment by migueldi...@gmail.com on 9 Jan 2008 at 5:51

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks, I'll go through it in depth as soon as possible.

One thing that comes to mind immediately, though:  Is the annotation really
necessary, or is it reasonable for AtUnit to just look for a field with a type 
that
isAssignableFrom(ApplicationContext.class) and inject its context there?

Original comment by logan.jo...@gmail.com on 9 Jan 2008 at 6:02

GoogleCodeExporter commented 8 years ago
Yes, it could be done looking directly for a suitable field to be injected. I'm 
just
preventing an scenario where the unit test has more than one such field.
I suppose that scenario would be very rare... but it's always better to be 
cautious.

An intermediate way to simplify things would be to search for the suitable 
fields and
choose the one that is annotated in case more than one exists.

Original comment by migueldi...@gmail.com on 9 Jan 2008 at 6:20

GoogleCodeExporter commented 8 years ago
Here is a version that doesn't require to annotate the spring context field,
alongside with the updated SpringContainer tests.
I've renamed @ContextInstance to @InjectedContext since it's more explanatory.
I'm not sure if the TooManyContextInstancesException name and package (it may be
better located in atunit.spring) are the best choice...

Original comment by migueldi...@gmail.com on 10 Jan 2008 at 10:09

Attachments: