When I run my Junit4 tests now I use the @RunWith(SpringJUnit4ClassRunner.class)
annotation which allows me to inject Spring Beans into my test class.
I would like to use the JMock 2 framework (which I have no real experience of) but examples I see require the following @RunWith(JMock.class)
.
So my question is can I use JMock and Spring together with my JUnit4 tests and if so how? For instance is there a Spring test runner that also supports JMock?
Thanks,
Richard
You can explicitly call context.assertIsSatisfied()
at the end of your test, in an @After
method for example.
On the jmock.org site, select the "Other" tab on any code example and the documentation will show you how to use jMock without JUnit framework integration.
The next version of jMock will support the new "rules" mechanism in JUnit 4.7, and so you won't need to use a custom test runner.
If the problem is related to @RunWith
, you can use the JMockit Expectations API instead.
It is similar to jMock in that expectations are also recorded inside a new Expectations() {...}
block, but does not require the use of @RunWith
(nor a base test class).
In addition, there is no context.checking(...)
method call wrapping the expectation block.
I think you might also be missing a point here. For unit testing, I want exact control over how the object under test is constructed, which means passing in known instances as collaborators. In other words, if I need an object then I create it in the test and pass it in. Invoking Spring seems like overkill.
On the other hand, I would need Spring beans if I were doing some kind of integration test, to see how the whole thing wired together or test against a third-party component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With