如何开始测试(jMock) [英] How to get started with testing(jMock)

查看:145
本文介绍了如何开始测试(jMock)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习如何编写测试。我也在学习Java,有人告诉我应该学习/使用/练习jMock,我在网上发现了一些有助于某些扩展的文章:

I'm trying to learn how to write tests. I'm also learning Java, I was told I should learn/use/practice jMock, I've found some articles online that help to certain extend like :

http://www.theserverside.com/news/1365050/使用-JMock-in-Test-Driven-Development

http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock

我发现的大多数文章都是关于测试的驱动开发,首先编写测试然后编写代码以使测试通过。我现在不是在寻找,我正在尝试使用jMock为现有代码编写测试。

And most articles I found was about test driven development, write tests first then write code to make the test pass. I'm not looking for that at the moment, I'm trying to write tests for already existing code with jMock.

官方文档至少对我来说是模糊的,对我来说太难了。有没有人有更好的方法来学习这个。好书/链接/教程对我很有帮助。谢谢

The official documentation is vague to say the least and just too hard for me. Does anybody have better way to learn this. Good books/links/tutorials would help me a lot. thank you

编辑 - 更具体的问题:

http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock - 来自这篇文章

http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock - from this article

尝试过这个来模拟这个简单的类:

import java.util.Map;
    public class Cache {
        private Map<Integer, String> underlyingStorage;
        public Cache(Map<Integer, String> underlyingStorage) {
            this.underlyingStorage = underlyingStorage;
        }
        public String get(int key) {
            return underlyingStorage.get(key);
        }
        public void add(int key, String value) {
            underlyingStorage.put(key, value);
        }
        public void remove(int key) {
            underlyingStorage.remove(key);
        }
        public int size() {
            return underlyingStorage.size();
        }
        public void clear() {
            underlyingStorage.clear();
        }
    }

以下是我尝试创建测试/模拟的方法:

Here is how I tried to create a test/mock :

public class CacheTest extends TestCase {

    private Mockery context;
    private Map mockMap;
    private Cache cache;

    @Override
    @Before
    public void setUp() {
        context = new Mockery() {
            {
                setImposteriser(ClassImposteriser.INSTANCE);
            }
        };

        mockMap = context.mock(Map.class);
        cache = new Cache(mockMap);
    }

    public void testCache() {
        context.checking(new Expectations() {{
            atLeast(1).of(mockMap).size(); 
            will(returnValue(int.class));
        }});

    }
}

它通过了测试,基本上没有没什么,我想要的是创建一个地图并检查它的大小,你知道工作的一些变化尝试抓住这个。通过实例了解更好,我在这里测试的其他内容或任何其他练习对我有很大的帮助。 tnx

It passes the test and basically does nothing, what I wanted is to create a map and check its size, and you know work some variations try to get a grip on this. Understand better trough examples, what else could I test here or any other exercises would help me a lot. tnx

推荐答案

这是一个关于使用JUnit和EasyMock的教程(我个人觉得比JMock更容易使用的模拟库): http://www.michaelminella.com/testing/unit -testing-with-junit-and-easymock.html

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

即使您100%致力于使用JMock,两者之间的概念也是相同的这应该可以帮助你更好地理解它们。

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.

模拟的目的是当你测试Class A 时,取决于 B C ,您对 A 的测试使用模拟版本的 B C 能够指定其确切行为,而不是使用<$ c的实际实现在您的 A 的测试中,$ c> B 和 C 。否则你不是只测试 A 的单个单元,你隐式测试 B C 以及。

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.

这篇关于如何开始测试(jMock)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆