对着官方文档,想开始写个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.
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:
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.
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.
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.
Here is the complete test.
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);
}
}
测试通过!