Questions tagged [jmock]

For questions about using jMock. JMock is a helper framework for test-driven development and unit testing.

According to the jMock project website,

JMock is a library that supports test-driven development of Java code with mock objects.

Mock objects help you design and test the interactions between the objects in your programs.

The jMock library:

  • makes it quick and easy to define mock objects, so you don't break the rhythm of programming
  • lets you precisely specify the interactions between your objects, reducing the brittleness of your tests
  • works well with the autocompletion and refactoring features of your IDE
  • plugs into your favourite test framework
  • is easy to extend
257 questions
3
votes
1 answer

jmock, return new object upon each call

I am setting up a mock object which is supposed to return a new business object each time I call a method f() on it. If I simply say returnValue(new BusinessObj()), it will return the same reference upon each call. If I do not know how many calls…
Bober02
  • 15,034
  • 31
  • 92
  • 178
3
votes
3 answers

JMock- java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch()

I understand that the solution is to somehow make sure that Junit is loaded after hamcrest. I have an intellij project, in which I setup an external library, which contains both JUnit and JMock and hamcrest. How can I make sure that this error does…
Bober02
  • 15,034
  • 31
  • 92
  • 178
3
votes
1 answer

JMock causes JUnit ExpectedException to pass even if an exception is not thrown

When I use JMock with JUnit ExpectedException the tests seem to pass even if the exception is not thrown. For example, the test below fails, as it should. But if I uncomment the two commented lines, it passes. Am I doing something wrong? Is there an…
robingrindrod
  • 474
  • 4
  • 18
2
votes
1 answer

Inject mock object using jMock

I'm trying to use jMock to create a mock object. The mock object looks fine - but the thing is it is not injected to the tested class. Here is my code: @RunWith(JMock.class) public class FeederFilterTest { private TestedClass…
Noam
  • 3,049
  • 10
  • 34
  • 52
2
votes
2 answers

Specify generic class type in argument matcher

Consider the following piece of code: final Foo foo = context.mock(Foo.class); context.checking(new Expectations() {{ one(foo).someMethod(with(aNonNull(List.class))); }}); I'm trying to suggest that someMethod is invoked with a non-null…
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
2
votes
4 answers

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 /…
TM.
  • 108,298
  • 33
  • 122
  • 127
2
votes
1 answer

Hamcrest matcher for Object... parameters

I got method of class interface Class1{ void method1(SomeObject... parameters); } I have a custom Hamcrest matcher public class SomeObjectMatcher extends BaseMatcher{...} How to write expectation matching that object passed to the…
Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
2
votes
1 answer

unable to use JMock after updating to Java 17

I'm updating our large old java desktop application from Java 11 to java 17. I've updated all the jars to the latest versions. jmock-2.12.0, cglib-3.3.0, asm-9.4. Working with JUnit 4 tests. A LOT of our JUnit tests are failing with…
CasaDelGato
  • 603
  • 7
  • 17
2
votes
2 answers

How does jmock and mockito create wrappers?

For example, I have the following code: SomeClass stub = Mockito.mock(SomeClass.class); After that, stub is a normal implementation of SomeClass, but with its own behavior (default is to just throw some exception, but that's ok) How can I do the…
Illarion Kovalchuk
  • 5,774
  • 8
  • 42
  • 54
2
votes
1 answer

How do I simulate the passage of time with java.time.Clock?

Suppose I have this class, ClassToBeTested, where the time elapsed between its method calls are important. foo returns a different value depending on whether you have called foo in the last x hours. The way I implemented this is to get the current…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
2
votes
1 answer

How do I use jMock's ClassImposteriser for Android unit testing?

In my unit test, I've tried the following: import org.jmock.Mockery; import org.jmock.Expectations; import org.jmock.lib.legacy.ClassImposteriser; public class MyActivityTest extends ActivityUnitTestCase { private Mockery context =…
Huey
  • 2,714
  • 6
  • 28
  • 34
2
votes
1 answer

How to use JMock to test mocked methods inside a mocked method

I'm having trouble determining how to mock a particular piece of code. Here is my method: public void sendNotifications(NotificationType type, Person person) { List notifications = findNotifications(type); …
AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
2
votes
1 answer

Let a test pass on a set JMock's mock method call

Is there any way in JMock2 to let a test pass when reaching the execution of a given mock's method call? In other words, I would like to write some test code like: assertTrue(when(aRequestMock).methodCall()); To test production code like: public…
LoreV
  • 575
  • 5
  • 25
2
votes
2 answers

jMock Expectations Not Specified

I'm new to jMock, so I'm trying it out on a simple example. I can't figure out why it's not working, though. Here's the class that I'm testing: package com.application; import com.domain.Coordinate; import com.domain.Playable; public class…
Mike
  • 19,267
  • 11
  • 56
  • 72
2
votes
1 answer

Using mock objects in multithreaded environment

Starting with jMock 2.6, I can make sure my mock objects are seen consistently by multiple threads via final Mockery mockery = new Mockery(); mockery.setThreadingPolicy(new Synchroniser()); What are my options (I'm experiencing intermittent test…
Bass
  • 4,977
  • 2
  • 36
  • 82