I'm currently thinking about expanding unit tests for a server and a client application. Mockito is already in use and - I think - very well suited for the task at hand. However, as the Mockito documentation itself admits:
Mockito is not an dependency injection framework, don't expect [the #InjectMocks annotation] to inject a complex graph of objects be it mocks/spies or real objects.
The server-side of course already has CDI via annotations at some places, the client will probably be extended in some place to use CDI for JavaSE in the near future. There is/will be a wild mix of field- and constructor-injection + #postconstruct methods, which is already too complicated for Mockito. So I'm looking for something that will allow me to easily use CDI annotations to inject Mockito's mocks/spies/real objects where needed.
Can Mockito's functionality be expanded via plugins or something similar to enable a dependency resolution closer to what is specified by CDI (I don't think I need the full spec, but something closer to it)? Is there another library that integrates with Mockito and JUnit5 that does that?
weld supports this out of the box, specifically by weld-junit. It supports both junit4 and junit5. In both cases one can define producer methods for the required injection points in which one can freely use mockito or powermock or any other mocking mechanism to create mocks which weld then injects into the test object.
If you wish to mock EJB/CDI beans with OpenEJB, you can do it very easily:
http://tomee.apache.org/master/examples/rest-applicationcomposer-mockito.html
cdi-unit and ioc-unit have modules which support that.
Just add
#Produces
#Mock
ClassName mockedObject;
and the framework will make it an injectable Bean.
Related
I need to execute block of code once upon startup on the Spock tests. I cannot use #Autowired in setupSpec() which is default method for such initialisation, however #Beans would not be loaded till that time.
Found on web (dating back to 2015) source :
The behavior is a consequence of the design of Spring's TestContext framework. I don't
see a way to change it without hitting other problems. The situation isn't any different
when using the TestContext framework with JUnit.
It's been 6 years already, is there any clean way to do this? I want to omit dirty workarounds
You are in luck, thanks to #erdi for implementing this in Add support for injection into #Shared fields in spock-spring module, you can try the feature in the Spock Snapshot 2.0 builds, and it will be in the Spock-2.0M5 release. You need to opt-in into #Shared injection via placing #EnableSharedInjection on your specification, also really important, that you read the javadoc and understand the mentioned implications of doing that.
I'm trying to set up a junit with spring and I'm trying to use spring's dependency injection to populate the test class. I'm wondering if this is something I should even be attempting? I think what I'm seeing is spring is instantiating the test class and performing the DI but then JUnit is creating it's own instance that hasn't had DI performed and the test is failing. I'm using JUnit 4.x and spring 3.1.1.
You can use spring to inject dependencies into your tests, thus making it an integration test. Annotate like this
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
#ContextConfiguration(locations = "/applicationContext-TEST.xml")
public class MyTest {}
But it can be preferable to just test your spring managed classes as pojo's and use mock objects where appropriate.
For example a lot of controller methods have a Model injected at runtime by Spring. However to unit test them I just pass in an HashMap instance. And my service layer classes I can pass in a mocked dao, which is easy because I designed to an interface and use setter injection...
With jUnit, each test should be isolated with no dependency outside of test coverage. There are several test frameworks available that provide mock bean instantiation in Spring.
There is an excellent Martin Fowler article on Stubs and Mocks to begin with.
Mockito in combination with PowerMock, can help you test spring components, services and controllers.
Mockito Intro: https://code.google.com/p/mockito/
PowerMock Intro: http://code.google.com/p/powermock/
I understand this will take up time to research, learn and implement, but this is very beneficial for writing jUnit tests with Dependency Injected beans.
Which mocking framework is preferred for testing EJB's (specifically Message Driven Beans), I'm looking at MockObjects, easyMock, MockEJB and mockito but open all.
I've not used mocking framework before and particularly interested in low learning curve / great getting started guide.
I should clarify, using EJB 2.
A framework specifically build for this is Arquillian - http://www.jboss.org/arquillian.
This lets you test in-container via JUnit. You can test individual beans or larger collections. You can provide mocks by simply packaging other implementations in the test archive.
When using Java EE 6 (JBoss AS 6, Glassfish V3), you can run the container in embedded mode which simplifies matters and saves you in run-time overhead.
EJB2 is notoriously difficult to test though. If possible I suggest you drop it completely in favor of EJB3.x, but of course this might not be an option for you.
It's not a mocking framework, but you might like to have a look at OpenEJB, which is a lightweight EJB container suitable for, amongst other things, use in unit tests. It will save you having to mock the container's interfaces, and you can still mock interfaces to other components.
If you're using EJB 3.x, then is heavily interface-based, and so your usual run-of-the-mill mocking frameworks will do just fine for unit tests; the fact that the code is used by the EJB framework isn't really relevant.
EJB 2.x is a different (and ugly) kettle of fish, though, since it doesn't conform to any of the usual sane rules of software design. Your best bet there is probably a framework like Cactus.
Is is possible to declare mocks using some mocking framework for my own classes declaratively with Spring? I know there are some standard mocks available in Spring, but I'd like to be able to mock out my own classes declaratively too.
Just to check I'm not going about this the wrong way: the idea is to have a pair of JUnit test and Spring config for each integration test I want to do, mocking everything except the specific integration aspect I'm testing (say I had a dependency on two different data services, test one at a time) and minimising the amount of repeated Java code specifying the mocks.
I did it using special context.xml that included the real XML and overwrote definition of the special beans. Id'd be happy to know that there is better and smarter solution but this one worked fine for me.
Seriously - you really dont want to be doing that.
I have seen a number of projects that attempt to do this and i promise that you will end up with
A huge number of spring files, each one slightly different, but you don't know what and why.
Spaghetti code, because the "declarative" definition doesn't allow to figure out that your objects are doing too much, or doing it with the wrong collaborators.
In the system case, there are a number of points at which you can stub out external services...
I would recommend that you read GOOS - It devotes a book to answering this kind of question.
http://www.growing-object-oriented-software.com/
If there is only a few beans that you want to change, and you want to change them for all tests, the you could have a look at the #Primary annotation.
You have to annotate the special class for the tests with #Primary - then it will "override" the real class. -- But use this only if you want to do it for all tests.
I am trying to use Guice for a test framework based on TestNG. This frameworks analyzes the test class for dependencies and provides them eliminating the need to build them in tests.
Guice is all about injection and I think is a good fit for the framework. But the question is how do I define bindings after I have created the injector? This is needed because tests may override bindings to substitute default implementations with mocks.
Besides that, I want to guess the implementation at runtime in some cases based on class names conventions. Sounds like Just-in-type binding feature. But how do I provide my own just-in-time binding provider?
That kind of dynamic behaviour isn't supported out-of-the-box, but you can achieve a lot with module rewriting. Take a look at Guiceberry, which already implements mock-substitution for JUnit tests. (And consider submitting a TestNG patch to them, they'd love that!)