jmock2:getting started with junit4

最近想玩玩JMock。
对着官方文档,想开始写个test case,不过让我郁闷的是官方文档上给的实例代码不完全。算了,自己写个跑跑看了。
1.测试接口:
IHelloService:
1 public interface IHelloService {
2
3     /**
4      * @param name
5      * @return hello message
6      */
7     String sayHelloToSomebody(String name);
8
9 }IMPL:
1 public class HelloServiceImpl implements IHelloService {
2
3     /*
4      * (non-Javadoc)
5      *
6      * @see org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String,
7      *      java.lang.String)
8      */
9     public String sayHelloToSomebody(String name) {
10         return "HELLO," + name + "!";
11     }
12
13 }Test Case:
1 public class IHelloServiceTest extends TestCase {
2     private Mockery context = new JUnit4Mockery();
3     private IHelloService helloService;
4
5     /**
6      * @throws java.lang.Exception
7      */
8     @Before
9     public void setUp() throws Exception {
10         // set up
11         helloService = context.mock(IHelloService.class);
12     }
13
14     /**
15      * Test method for
16      * {@link org.hook.jmock.firstcase.HelloServiceImpl#sayHelloToSomebody(java.lang.String)}.
17      */
18     @Test
19     public void testSayHelloToSomebody() {
20         final String message = "HELLO,alex!";
21         final String name = "alex";
22         // expectations
23         context.checking(new Expectations() {
24             {
25                 one(helloService).sayHelloToSomebody(name);
26                 will(returnValue(message));
27             }
28         });
29         // execute
30         String result = helloService.sayHelloToSomebody(name);
31         // verify
32         context.assertIsSatisfied();
33         assertSame(result, message);
34     }
35 }OK,跑下测试,green bar...
2.测试类:
HelloService:
1 public class HelloService {
2
3     /**
4      * @param name
5      * @return hello message
6      */
7     public String sayHelloToSomebody(String name) {
8         return "HELLO," + name + "!";
9     }
10 }Test Case:
1 public class HelloServiceTest extends TestCase {
2     private Mockery context;
3     private HelloService helloService;
4
5     /**
6      * @throws java.lang.Exception
7      */
8     @Before
9     public void setUp() throws Exception {
10         context = new JUnit4Mockery();
11         // 声明针对类进行mock,针对接口则会采用动态代理,不需要声明
12         context.setImposteriser(ClassImposteriser.INSTANCE);
13         helloService = context.mock(HelloService.class);
14     }
15
16     /**
17      * Test method for
18      * {@link org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String)}.
19      */
20     @Test
21     public void testSayHelloToSomebody() {
22         final String message = "HELLO,vivian!";
23         final String name = "vivian";
24         // expectations
25         context.checking(new Expectations() {
26             {
27                 one(helloService).sayHelloToSomebody(name);
28                 will(returnValue(message));
29             }
30         });
31         // execute
32         String result = helloService.sayHelloToSomebody(name);
33         // verify
34         context.assertIsSatisfied();
35         assertSame(result, message);
36     }
37 }

你可能感兴趣的:(java,UP)