如何开始测试(jMock)
我正在尝试学习如何编写测试。我也在学习Java,有人告诉我应该学习/使用/练习jMock,我在网上找到了一些文章,在一定程度上有帮助,例如:
http://www.theserverside.com/news/1365050/Using-JMock-in-Test-Driven-Development
http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock
以及大多数文章我发现关于测试驱动开发,先编写测试,然后编写代码以使测试通过。我现在不是在寻找这个,我正在尝试使用 jMock 为已经存在的代码编写测试。
官方文档至少可以说是模糊的,对我来说太难了。有没有人有更好的方法来学习这个。好的书籍/链接/教程会对我有很大帮助。谢谢
编辑 - 更具体的问题:
http://jeantessier.com /SoftwareEngineering/Mocking.html#jMock - 来自这篇文章
尝试用这个来模拟这个简单的类:
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();
}
}
这是我尝试创建测试/模拟的方法:
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
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/Using-JMock-in-Test-Driven-Development
http://jeantessier.com/SoftwareEngineering/Mocking.html#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
EDIT - more concrete question :
http://jeantessier.com/SoftwareEngineering/Mocking.html#jMock - from this article
Tried this to mock this simple class :
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));
}});
}
}
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是有关使用 JUnit 和 EasyMock(我个人认为比 JMock 更容易使用的模拟库)的教程: http://www.michaelminella.com/testing/unit-testing-with-junit-and-easymock.html
即使您 100% 致力于使用 JMock ,两者之间的概念是相同的,这应该可以帮助您更好地理解它们。
模拟的目的是,当您测试依赖于
B
和C
的类A
时,您对A
的测试> 使用B
和C
的模拟版本来指定它们的确切行为,而不是使用B
和C 的真实实现
在您的A
测试中。否则,您不仅会测试A
的单个单元,还会隐式测试B
和C
。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 onB
andC
, your test ofA
uses mock versions ofB
andC
to be able to specify their exact behavior rather than using the real implementations ofB
andC
in your test ofA
. Otherwise you are not testing just the single unit ofA
, you are implicitly testingB
andC
as well.作为 JMock 的作者,除非您有一定的 TDD 经验,否则我不会开始使用该技术。只需从基础开始并让它发挥作用。一旦你开始遇到规模和设计发展方面的困难,就回到技术上来。
戴夫·阿斯特尔斯的书仍然是一本很好的介绍性书籍,我认为是那一代人中唯一一本很好地解释了嘲笑的书。之后,您可能会(咳咳)考虑我们的“以测试为指导,发展面向对象的软件”,
如果有人告诉您这一切都是为了使针对文件系统的测试运行得更快,那么您可能会打折扣。
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.
您不需要真正的模拟来测试此类,因为它的唯一合作者是一个 Map,您也可以按原样使用它。此外,您的班级实际上并没有做任何事情(除了委托),这就是为什么您感觉自己没有进行太多测试。
直接测试可能是(我假设您正在使用 JUnit 4 - 您的代码是 JUnit 3 和 4 与模拟的奇怪混合
假设模拟代码是正确的 - 我不使用 JMock)
( 通过将地图设置为具有要测试的属性来设置系统,然后检查缓存是否具有相同的属性(因为它是直接委托)。
在这两种情况下,您都 /junit.sourceforge.net/" rel="nofollow noreferrer">关于 JUnit,然后再继续。
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
with mocks it would be (assuming the mock code is correct -- I don't use JMock)
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.
我不知道您在学习如何在测试中使用模拟对象方面已经走了多远,所以我将写一个简短的描述,然后为您指出一篇可能对您有帮助的文章的方向。模拟对象在单元测试中用于替换难以创建或难以进入您希望它们进行测试的状态的外部依赖项。现有的各种模拟框架为您提供了创建“假”对象来代替这些依赖项的机制。这些模拟对象将跟踪从代码中进入它们的调用,并允许您稍后对这些交互进行断言。有一篇关于模拟对象以及它们如何与“存根”相关的众所周知的文章,“存根”是另一种简化外部依赖关系的常见测试策略。它由 Martin Fowler 编写,可以在这里找到:
http://martinfowler.com/articles/mocksArentStubs。 html
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