tominich | 26 Jun 2012 01:19

Re: ignoring except never for one method


I don think so.  Perhaps an actual example with all the code might help
explain:

In this example I am mocking Piston and don't want engine to call the
piston's snap() method when it starts, but I don't care what other methods
on piston it calls.  I could declare all the individual methods that
starting the engine calls on piston, but that makes the test fragile when I
change the engine implementation and add further calls to piston.  

The test below passes, but I want it to fail because the engine is calling
snap on piston

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test;

public class MockNever {
     <at> Test
    public void testMockNever() {

        // setup
        Mockery mockery = new Mockery();
        final Engine engine = new Engine();

        final Piston piston = mockery.mock(Piston.class);
        engine.setPiston(piston);

        // set the expectations
        mockery.checking(new Expectations() {{
(Continue reading)

Andy Law | 26 Jun 2012 11:15
Picon
Picon
Favicon

Re: ignoring except never for one method


tominich wrote:
> 
> I don think so.  Perhaps an actual example with all the code might help
> explain:
> 
> 

Ahh. I see. No, I can't think of a way to use States to do that.

Looks like you'll have to write a Matcher.

I think that the following works. Have a brief look at
http://www.jmock.org/custom-matchers.html for how to tidy it up.

import java.lang.reflect.Method;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.jmock.Expectations; 
import org.jmock.Mockery;
import org.junit.Test;

public class MockNever { 
     <at> Test 
    public void testMockNever() { 

        // setup 
        Mockery mockery = new Mockery(); 
        final Engine engine = new Engine(); 

(Continue reading)

tominich | 28 Jun 2012 12:52

Re: ignoring except never for one method


Thanks Andy, that works

The only small problem I have with it is that we refer to the method as a
string "snap" so that refactoring the code such as changing the snap method
name will probably miss it and need a manual change here too

But, thanks a lot I will use that

Andy Law wrote:
> 
> 
> tominich wrote:
>> 
>> I don think so.  Perhaps an actual example with all the code might help
>> explain:
>> 
>> 
> 
> Ahh. I see. No, I can't think of a way to use States to do that.
> 
> Looks like you'll have to write a Matcher.
> 
> I think that the following works. Have a brief look at
> http://www.jmock.org/custom-matchers.html for how to tidy it up.
> 
> 
> import java.lang.reflect.Method;
> import org.hamcrest.Description;
> import org.hamcrest.TypeSafeMatcher;
(Continue reading)


Gmane