(1)jmock测试入门

最近想玩玩JMock。

对着官方文档,想开始写个test case,不过让我郁闷的是官方文档上给的实例代码不完全


这个是官方的文档:

地址是:http://jmock.org/getting-started.html

Getting Started

In this simple example we are going to write a mock object test for a publish/subscribe message system. A Publisher sends messages to zero or more Subscribers. We want to test the Publisher, which involves testing its interactions with its Subscribers.

The Subscriber interface looks like this:

interface Subscriber {
    void receive(String message);
}

We will test that a Publisher sends a message to a single registered Subscriber. To test interactions between the Publisher and the Subscriber we will use a mock Subscriber object.

Set Up the Class Path

To use jMock 2.6.0 you must add the following JAR files to your class path:

  • jmock-2.6.0.jar
  • hamcrest-core-1.3.jar
  • hamcrest-library-1.3.jar

Write the Test Case

First we must import the jMock classes, define our test fixture class and create a "Mockery" that represents the context in which the Publisher exists. The context mocks out the objects that the Publisher collaborates with (in this case a Subscriber) and checks that they are used correctly during the test.

Show for:  JUnit 3 JUnit 4 Other
import org.jmock.Mockery;
import org.jmock.Expectations;

public class PublisherTest extends TestCase {
    Mockery context = new Mockery();
    ...    
}

This is a JUnit 3 test case but apart from the test case class the code will be the same when using any test framework for which jMock 2 does not have an integration layer.

Now we want to write the method that will perform our test:

Show for:  JUnit 3 JUnit 4 Other
public void testOneSubscriberReceivesAMessage() {
    ...
}

We will now write the body of the test method.

We first set up the context in which our test will execute. We create a Publisher to test. We create a mock Subscriber that should receive the message. We then register the Subscriber with the Publisher. Finally we create a message object to publish.

Show for:  JUnit 3 JUnit 4 Other
final Subscriber subscriber = context.mock(Subscriber.class);

Publisher publisher = new Publisher();
publisher.add(subscriber);

final String message = "message";

Next we define expectations on the mock Subscriber that specify the methods that we expect to be called upon it during the test run. We expect the receive method to be called once with a single argument, the message that will be sent.

Show for:  JUnit 3 JUnit 4 Other
context.checking(new Expectations() {{
    oneOf (subscriber).receive(message);
}});

We then execute the code that we want to test.

publisher.publish(message);

After the code under test has finished our test must verify that the mock Subscriber was called as expected. If the expected calls were not made, the test will fail.

Show for:  JUnit 3 JUnit 4 Other
context.assertIsSatisfied();

Here is the complete test.

Show for:  JUnit 3 JUnit 4 Other
import org.jmock.Mockery;
import org.jmock.Expectations;

public class PublisherTest extends TestCase {
    Mockery context = new Mockery();

    public void testOneSubscriberReceivesAMessage() {
        // set up
        final Subscriber subscriber = context.mock(Subscriber.class);

        Publisher publisher = new Publisher();
        publisher.add(subscriber);
        
        final String message = "message";
        
        // expectations
        context.checking(new Expectations() {{
            oneOf (subscriber).receive(message);
        }});

        // execute
        publisher.publish(message);
        
        // verify
        context.assertIsSatisfied();
    }
}


本人敲的代码:

package com.utstar.omco.appserver.test;

public class Publisher {

	Subscriber subscriber;
	
	public void add(Subscriber subscriber){
		this.subscriber = subscriber;
	}

	public String publish(String message) {
		subscriber.receive(message);
		  return  message  ;
	}
	

}


package com.utstar.omco.appserver.test;

interface Subscriber {
    void receive(String message);
}


package com.utstar.omco.appserver.test;
import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;

public class PublisherTest extends TestCase {
    Mockery context = new Mockery();

    public void testOneSubscriberReceivesAMessage() {
        // set up
        final Subscriber subscriber = context.mock(Subscriber.class);
  //我们创建一个出版者去测试,我们创建一个订阅者能够收到消息,我们注册订阅者到出版商,最后我们创建消息取公布
        Publisher publisher = new Publisher();
        publisher.add(subscriber);
        
        final String message = "message";
        
        // expectations
        context.checking(new Expectations() {{
            oneOf (subscriber).receive(message);
        }});

        // execute
        // execute
        String result =    publisher.publish(message);
        
        // verify
        context.assertIsSatisfied();
        
        assertSame(result, message);
    }
}


测试通过!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值