easymock

PowerMock's expectNew() isn't mocking a constructor as expected

微笑、不失礼 提交于 2019-11-29 13:17:26
I'm trying to learn the ins and outs of various mocking libraries and PowerMock (specifically the EasyMock extension) is next on the list. I'm attempting to mock a constructor and the examples provided don't have the same response when I try to replicate them. As far as I can tell, it never mocks the constructor and just proceeds as if it were normal. This is the test class: @RunWith(PowerMockRunner.class) @PrepareForTest({Writer.class}) public class FaultInjectionSituationTest { @Test public void testActionFail() throws Exception { FaultInjectionSituation fis = new FaultInjectionSituation();

Mock Runtime.getRuntime()?

孤人 提交于 2019-11-29 11:46:19
问题 Can anyone make any suggestions about how best to use EasyMock to expect a call to Runtime.getRuntime().exec(xxx) ? I could move the call into a method in another class that implements an interface, but would rather not in an ideal world. interface RuntimeWrapper { ProcessWrapper execute(String command) throws IOException; } interface ProcessWrapper { int waitFor() throws InterruptedException; } I was wondering if anyone had any other suggestions? 回答1: Your class shouldn't call Runtime

Getting EasyMock mock objects to throw Exceptions

怎甘沉沦 提交于 2019-11-29 03:04:24
I'm in process of using EasyMock to write Unit tests for a number of collaborating classes. One of these classes (lets call it Foo ) opens a network connection to a remote server and parses that servers' XML response into something the rest of the classes can use. Presently my tests only encompass scenarios in which everything is hunky-dory and the remote server is up and running and returning XML as expected. However, I would be happier if I could mock Foo so that I simulate what happens if the remote server is down, or there is some other problem that causes an IOException to be thrown by

EasyMock andReturn() vs andStubReturn()

夙愿已清 提交于 2019-11-28 19:04:46
What is the difference between using andReturn(T value) vs andStubReturn(T value) for EasyMock? In what situation would you use andStubReturn() where andReturn() can't achieve the same result? You use a stub return for a method call on the mock that you expect to happen but aren't otherwise interested in. You use a regular return for a "regular" method call. Consider the following method: public void someMethod(String arg) { if (logger.isDebugEnabled()) { logger.debug("Calling doSomething() on service " + service.getName().hashCode()); } service.postMessage("{" + arg + "}"); if (logger

EasyMock vs Mockito: design vs maintainability? [closed]

蓝咒 提交于 2019-11-28 16:11:38
One way of thinking about this is: if we care about the design of the code then EasyMock is the better choice as it gives feedback to you by its concept of expectations. If we care about the maintainability of tests (easier to read, write and having less brittle tests which are not affected much by change), then Mockito seems a better choice. My questions are: If you have used EasyMock in large scale projects, do you find that your tests are harder to maintain? What are the limitations of Mockito (other than endo testing)? Henri I'm an EasyMock developer so a bit partial but of course I've

Test that void method didn't get called with EasyMock

给你一囗甜甜゛ 提交于 2019-11-28 10:51:53
Is this possible? I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1 You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer . with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail: Expectation failure on verify: myMethod(): expected: 1, actual: 0` based on nkr1pt example: expectLastCall().andAnswer(new IAnswer() { public Object answer() { Assert.assertFail(); return null; } }).anyTimes(); The fact that some

NoClassDefFoundError when using Powermock

故事扮演 提交于 2019-11-28 08:57:14
I'm running a junit test case using the PowerMock test runner. I'm using the following command line to execute it: java -cp .:junit-4.9b2.jar:easymock-3.0.jar:powermock-easymock-1.4.8-full.jar org.junit.runner.JUnitCore SampleTest When doing so I am receiving this error: initializationError(SampleTest) java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner ... How can I fix it? I just solved this one now, when I added the @RunWith(PowerMockRunner.class) attribute, eclipse automatically imported: import org.powermock.modules.junit4.legacy.PowerMockRunner; All I needed to do

PowerMock's expectNew() isn't mocking a constructor as expected

邮差的信 提交于 2019-11-28 07:10:28
问题 I'm trying to learn the ins and outs of various mocking libraries and PowerMock(specifically the EasyMock extension) is next on the list. I'm attempting to mock a constructor and the examples provided don't have the same response when I try to replicate them. As far as I can tell, it never mocks the constructor and just proceeds as if it were normal. This is the test class: @RunWith(PowerMockRunner.class) @PrepareForTest({Writer.class}) public class FaultInjectionSituationTest { @Test public

How to mock a static final variable using JUnit, EasyMock or PowerMock

对着背影说爱祢 提交于 2019-11-27 19:45:08
I want to mock a static final variable as well as mock a i18n class using JUnit, EasyMock or PowerMock. How do I do that? Antoine Is there something like mocking a variable? I would call that re-assign. I don't think EasyMock or PowerMock will give you an easy way to re-assign a static final field (it sounds like a strange use-case). If you want to do that there probably is something wrong with your design: avoid static final (or more commonly global constants) if you know a variable may have another value, even for test purpose. Anyways, you can achieve that using reflection (from: Using

EasyMock: Void Methods

谁说胖子不能爱 提交于 2019-11-27 17:14:27
I have a method that returns void in a class that is a dependency of the class I want to test. This class is huge and I'm only using this single method from it. I need to replace the implementation of this method for the test as I want it to do something different and I need to be able to access the parameters this method receives. I cannot find a way of doing this in EasyMock . I think I know how to do it with Mockito by using doAnswer but I don't want to add another library unless absolutely necessary. matt b If I understand what you want to do correctly, you should be able to use andAnswer(