Using Spring JUnit4 and JMock together

When I run Junit4 tests, I use an annotation @RunWith(SpringJUnit4ClassRunner.class)that allows me to embed Spring Beans in my test class.

I would like to use the JMock 2 framework (which I have no real experience), but for the examples I see, the following is required @RunWith(JMock.class).

So my question is: can I use JMock and Spring along with my JUnit4 tests, and if so, how? For example, is there a Spring test runner that also supports JMock?

Thanks,

Richard

+5
source share
3 answers

You can explicitly call context.assertIsSatisfied()at the end of the test, for example, at @After.

jmock.org "" , , jMock JUnit.

jMock "" JUnit 4.7, .

+6

@RunWith, JMockit Expectations API. jMock , new Expectations() {...}, @RunWith ( ). , context.checking(...), .

+1

I think you can also skip this point. For unit testing, I want to precisely control how the object is built under the test, which means the transfer in certain cases as co-authors. In other words, if I need an object, I will create it in the test and pass it. Spring's call seems redundant.

On the other hand, I will need Spring beans if I were to do some kind of integration test to see how all this is connected or to test on a third-party component.

+1
source

All Articles