How to get started with testing(jMock)

≯℡__Kan透↙ 提交于 2019-12-03 13:47:07

Here is a tutorial about using JUnit and EasyMock (a mocking library I personally find far easier to use than JMock): http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html

Even if you are 100% dedicated to using JMock, the concepts between the two are the same and this should help you understand them better.

The purpose of mocking is that when you test Class A, which depends on B and C, your test of A uses mock versions of B and C to be able to specify their exact behavior rather than using the real implementations of B and C in your test of A. Otherwise you are not testing just the single unit of A, you are implicitly testing B and C as well.

As an author of JMock, I wouldn't start with the technique until you have some experience with TDD. Just start with the basics and get it working. Once you start to experience difficulties with scale and growing a design, come back to the technique.

The Dave Astels book is still a good introduction and the only one, I think, of that generation that explained mocks well. After that, you might (ahem) consider ours, "Growing Object Oriented Software, Guided by Tests"

Discount anyone who tells you it's all about making tests against the file system go faster.

You don't need really mock to test this class as its only collaborator is a Map which you might as well just use as is. Also your class doesn't really do anything (except delegate) which is why you feel like you are not testing much.

A straight test might be (I'm assuming you are using JUnit 4 -- your code is an odd mixture of JUnit 3 and 4

@Test
public void sizeIs0WhenEmpty()
{
  Map<Integer, String> map = Collections.emptyMap();
  Cache cache = new Cache(map)
  assertEquals(0, cache.size());
}

with mocks it would be (assuming the mock code is correct -- I don't use JMock)

@Test
public void sizeIs0WhenEmpty()
{
  context.checking(new Expectations() {{
                   atLeast(1).of(mockMap).size(); 
                   will(returnValue(0));
                   }});
  assertEquals(0, cache.size());
}

In both cases you setup the system by setting the map to have the properties you want to test and then check that the cache has the same properties (as it is a straight delegate).

I would recommend you read about JUnit before you continue.

I don't know how far you've gone down the path to learning about using mock objects in testing, so I'll write a brief description then point you in the direction of an article that may be helpful to you. Mock objects are used in unit testing to replace external dependencies that are difficult to create or difficult to get into the state you want them for your test. The various mocking frameworks that exist give you mechanisms to create "fake" objects that take the place of these dependencies. These mock objects will keep track of calls coming into them from your code and allow you to make assertions about these interactions later. There's a well known article about mock objects and how they relate to "stubs", another common testing strategy for simplifying external dependencies. It was written by Martin Fowler and can be found here:

http://martinfowler.com/articles/mocksArentStubs.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!