jmock

jmock mocking a static method

Hello all. I have a static method in my code that I would like somehow to mock. I am using jmock. One way I suppose I could do this is to have "wrapper class" around the static method and mock this but I was hoping for a better solution. I am going about this the wrong way? FEEDBACK: I was going to have a interface and class that h...

How is Jmock used with HttpSession and HttpServletRequest

I am new to jmock and trying to mock an HttpSession. I am getting: java.lang.AssertionError: unexpected invocation: httpServletRequest.getSession() no expectations specified: did you... - forget to start an expectation with a cardinality clause? - call a mocked method to specify the parameter of an expectation? the test method: @Tes...

Get/save parameters to an expected JMock method call?

Hi, I want to test an "Adapter" object that when it receives an xml message, it digest it to a Message object, puts message ID + CorrelationID both with timestamps and forwards it to a Client object.=20 A message can be correlated to a previous one (e.g. m2.correlationID =3D m1.ID). I mock the Client, and check that Adapter successfull...

Why do you use .proxy() in JMock?

Hi, I'm trying to learn how to use JMock and I'm a bit confused about what you use the .proxy() method for. I know its invoked on your Mock class but I don't understand what its puprose is. I haven't had any luck finding a good description about how it works on google. Any help is much appreciated. ...

Mock Struts FormFile

I have an action class that expects a FormFile from a DynaActionForm. Typically in JMock, I'll just add request params to the struts mock, which will in turn populate the DynaActionForm. Obviously, the set request param is for strings only. So I'm unsure how to get or set the mock to use a multi-part request such that I can add the Fo...

Can I mock a super class method call?

Sometimes, you want to test a class method and you want to do an expectation on a call of a super class method. I did not found a way to do this expectation in java using easymock or jmock (and I think it is not possible). There is a (relative) clean solution, to create a delegate with the super class method logic and then set expectati...

Testing a Concrete class with Jmock

I have class with a forwarding method foo: void foo( Concrete c, String s ) { c.bar( s ); } I wish to test that foo in fact forwards. Unfortunately for me, Concrete is a class in a third-party library, and is a Concrete type, not an interface. Thus I must use ClassImposterizer in Jmock to mock Concrete, so in my testcase I do this: @...

Creating a mock with Jmock to return a null value for itself

I have a method that looks similar to the following: public void myMethod(MyClass c) { if (c == null) { return; } try { c.someMethod(); } catch (SomeException e) { // log the exception, possibly re-throw } } I am trying to find a way to set up a mock instance of the MyClass parameter c such that it return...

jUnit - How to assert that inherited methods are invoked?

Let's say you have some 3rd-party library class that you want to extend, simply to add convenience methods to it (so you can call an inherited method with default parameters for example). Using jUnit/jMock, is it possible to write an assertion / mock expection that tests that the correct inherited method is called? For example, somethi...

jMock Mocking Classes and Interface

I was experimenting jMock as my mocking framework for my project. I came into a situation where I need to mock both a class and an interface. I used the ClassImposteriser.INSTANCE to initiate the impostor of the context. Supposing a class Validator and an interface Person to mock. When I was going to mock the Interface Person, I ran to ...

Is there a neater way of testing calls to mocked methods for each item in a list

This is an example of a pattern I've encountered a lot recently. I have a method to be tested that takes a List and may invoke some other method(s) for each item in the list. To test this I define an Iterator with the expected call parameters and a loop in the JMock expectations to check the call is made against each item of the iterator...

Customize formatting of passed-in parameter in an expectation

I'm using jmock to mock out an OutputStream and set expectations on the data that gets written to it. So I have an expectation that looks something like this oneOf(stream).write(byteArrayMatching("Some string")); (byteArrayMatching) is a factory for a custom matcher. This all works fine, except when the test fails because the class ...

Lack of support base class in Junit4/Jmock2

We're finally migrating our unit test code base from JUnit 3 to JUnit 4. We also make heavy use of JMock 2. With JUnit 3, JMock provides a useful base class for your tests (MockObjectTestCase), which as well as itself being s subclass of Junit's TestCase, it handles various housekeeping duties regarding the mock framework. It makes lif...

How to mock a static variable in java using JMock

Hello, I have a Unit testing problem where a class has a static variable which wants to load the Spring Application Ctx. This class DOES NOT come out of the Bean Factory and I cannot change this fact. static ApplicationContext applicationContext = ...; This works fine, but is hard to JMock, or atleast I don't know a way and until I...

Unit testing a Swing component

I am writing a TotalCommander-like application. I have a separate component for file list, and a model for it. Model support listeners and issues a notification for events like CurrentDirChanged etc. in following manner: private void fireCurrentDirectoryChanged(final IFile dir) { if (SwingUtilities.isEventDispatchThread()) for...

Using Spring JUnit4 and JMock together

When I run my Junit4 tests now I use the @RunWith(SpringJUnit4ClassRunner.class) annotation which allows me to inject Spring Beans into my test class. I would like to use the JMock 2 framework (which I have no real experience of) but examples I see require the following @RunWith(JMock.class). So my question is can I use JMock and Sprin...

AtUnit vs 'Junit,JMock and GUICE' by hand - ?

How does AtUnit fare with respect to unit testing using DI and guice ?. Please share your experiences. ...

Unit test helper methods?

Hi, I have classes which prviously had massive methods so i subdivided the work of this method into 'helper' methods. These helper methods are declared private to enforce encapsulation - however I want to unit test the big public methods, is it good to unit test the helper methods too as if one of them fail the public method that calls i...

What is the best way to use Guice and JMock together?

I have started using Guice to do some dependency injection on a project, primarily because I need to inject mocks (using JMock currently) a layer away from the unit test, which makes manual injection very awkward. My question is what is the best approach for introducing a mock? What I currently have is to make a new module in the unit t...

[junit] No tests found using JMock

having problems trying to run by unit test with Ant, my test class uses Jmock; @RunWith(JMock.class) and annotations to identify each test method. When i attempt to build with ant (1.7.1) i get a [junit] No tests found in MyTestClass [junit] junit.framework.AssertionFailedError: No tests found Any suggestions? ...