Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Sharing/Reusing Expectations across tests.

Chris Miles

2009-07-29

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,

When using the TestNG testing framework, I would like to use TestNG's
"dependsOnMethods" property to allow a test to rely on another test.

Say my class under test, and the method being tested does two sequential
events

public void process() {
  object1.doThis();
  object2.doThat();
}

So say I want to write two tests (correct me if there is a better design
here) to test that my class communicates with these two unrelated
collaborators.

Without test dependencies I would have 2 simple tests like this.

  @Test(groups = "unit")
  public void testDoThisCalled() {
    this.mockery.checking(new Expectations() {
       {
          oneOf(object1).doThis();
          ignoring(object2).doThat();
       }
    });

    this.myClass.process();
  }

  @Test(groups = "unit")
  public void testDoThatCalled() {
    this.mockery.checking(new Expectations() {
       {
          ignoring(object1).doThis();
          oneOf(object2).doThat();
       }
    });

    this.myClass.process();
  }

Each test tests the object in question, but I think it gets very messy
having to ignore all of the other objects. There is only two objects here,
but imagine ten or more, and having to ignore them for every test it gets
even more messy.

Now TestNG allows you to have one test as a dependent on another test, i.e
the setUp portion (@(protected)
results of the first test can be used as part of the second test.

It would be good to be able to do something like

  @Test(groups = "unit")
  public void testDoThisCalled() {
    this.mockery.checking(new Expectations() {
       {
          oneOf(object1).doThis();
          ignoring(object2).doThat();
       }
    });

    this.myClass.process();
  }

  @Test(groups = "unit", dependsOnMethods = "testDoThisCalled")
  public void testDoThatCalled() {
    this.mockery.checking(new Expectations() {
       {
          oneOf(object2).doThat();
       }
    });

    this.myClass.process();
  }

Where the oneOf(object2).doThat() in the second test overwrites the
ignoring(object2).doThat() in the first test, and the
oneOf(object1).doThis() in test one is added to the expectations of the
second test removing the need to add it again. In this second example
TestNG runs test 1 and test 2 in the same sequence without reinitialising
things so I would like the exectations to be carried to the second test (I
hope that make sense)

Again, if there is a cleaner way of doing this I would be greatly
apreciated if I could be pointed in the right direction.

Best Regards,

Chris

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.