删除之前在 JMockit 中定义的期望
我有一个对象,我在测试类的 @Before
/setUp()
方法中使用 JMockit NonStrictExcpection()
进行模拟,所以它返回被测试类正常执行的预期值。
这对于我的所有测试方法来说都很好,除了我想测试此代码的非正常操作的单个测试。
我尝试在测试方法中创建一个新的期望,我相信这会覆盖 setUp 方法中的期望,但我发现 setUp 方法中的期望正在抑制新的期望。
当我删除设置期望时,测试方法的行为符合预期(但我的所有其他测试自然都会失败)。
我应该如何编码我的测试类,以便我可以用最少的代码为每个测试正确定义期望?(我知道我可以将期望代码复制/粘贴到每个测试方法中,< em>但我不想这样做(如果可以避免的话)。
我的测试代码看起来像这样(注意,这是某种伪代码并且无法编译,但你明白了):
public class TestClass{
@Before
public void setUp(){
// Here I define the normal behaviour of mockObject
new NonStrictExpectations() {{
mockObject.doSomething();
result = "Everyting is OK!";
}};
// Other set up stuff...
}
// Other Tests...
/**
* This method tests that an error when calling
* mockObject.doSomething() is handled correctly.
*/
@Test(expected=Exception.class)
public void testMockObjectThrowsException(){
// This Expectation is apparently ignored...
new NonStrictExpectations() {{
mockObject.doSomething();
result = "Something is wrong!";
}};
// Rest of test method...
}
}
I have an object that I'm mocking with a JMockit NonStrictExcpection()
in the @Before
/setUp()
method of my test class so that it returns the value expected for normal execution of my class under test.
This is fine for all of my test methods save for a single test where I want to test non-normal operation of this code.
I have tried creating a new expectation in the test method, which I believed would override the expectation in the setUp method, but I have found that the expectation in the setUp method is suppressing the new expectation.
When I remove the setUp expectation, the test method behaves as expected (but all my other tests fail, naturally).
How should I code my test class so that I can have the expectations correctly defined for each test with the minimum amount of code? (I know I could copy/paste the expectation code into each test method, but I don't want to do that if at all avoidable).
My test code looks something like this (note, this is sorta psuedocode and doesn't compile, but you get the idea):
public class TestClass{
@Before
public void setUp(){
// Here I define the normal behaviour of mockObject
new NonStrictExpectations() {{
mockObject.doSomething();
result = "Everyting is OK!";
}};
// Other set up stuff...
}
// Other Tests...
/**
* This method tests that an error when calling
* mockObject.doSomething() is handled correctly.
*/
@Test(expected=Exception.class)
public void testMockObjectThrowsException(){
// This Expectation is apparently ignored...
new NonStrictExpectations() {{
mockObject.doSomething();
result = "Something is wrong!";
}};
// Rest of test method...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常只创建一个返回
Expectations
类型的私有方法:然后只需在需要确切期望的测试方法中调用该方法:
I usually just make a private method which returns an
Expectations
type:And then just call the method in the test methods that need the exact expectation:
您可以更好地使用 MockUp 进行基于状态的测试。在每个测试方法中定义您想要的模型。您可以在每次测试结束时调用MockUp的tearDown方法来删除mock。
此处进行了说明
You can better use MockUp for state based testing. Define the Mockup you want in each test method. You can call the tearDown method of MockUp to remove the mock at the end of each test.
It is described here