在上面篇文章,我们的测试代码如下:

 1 None.gif package  com.martin.pdo;
 2 None.gif
 3 None.gif import  org.springframework.context.ApplicationContext;
 4 None.gif
 5 None.gif import  junit.framework.Assert;
 6 None.gif import  junit.framework.TestCase;
 7 None.gif
 8 ExpandedBlockStart.gifContractedBlock.gif public   class  UserTest  extends  TestCase  dot.gif {
 9InBlock.gif
10ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testUser() dot.gif{
11InBlock.gif        ApplicationContext context = JUnitTestHelper.getContext();
12InBlock.gif        User user = (User) context.getBean("user");
13InBlock.gif                User _u1 = new User();
14InBlock.gif        _u1.setName("martin xus");
15InBlock.gif        _u1.setSex('F');
16InBlock.gif        _u1.setAge(100);
17InBlock.gif        user.add(_u1);
18InBlock.gif
19InBlock.gif        Assert.assertNotNull(_u1.getId());
20InBlock.gif
21InBlock.gif        User _u2 = (User) user.loadByName("martin xus").get(0);
22InBlock.gif        Assert.assertEquals(_u1.getId(), _u2.getId());
23InBlock.gif
24InBlock.gif        user.remove(_u2);
25InBlock.gif        Assert.assertTrue(user.loadByName("martin xus").size() == 0);
26ExpandedSubBlockEnd.gif    }

27InBlock.gif
28ExpandedBlockEnd.gif}

29 None.gif


而这些,必须要和spring&hibernate一起工作,必须保证数据库操作正确,而我们需要的只是测试这个类的本身!
有必要连hibernate,spring一起测试吗?当然没有,测试,只需要保证本代码完成相应的功能,即可.

那么就有必要更换测试方法:
mock便是首选,在这里我们选择了jmock (http://www.jmock.org)

 1 None.gif package  com.martin.mock;
 2 None.gif
 3 None.gif import  org.jmock.Mock;
 4 None.gif import  org.jmock.MockObjectTestCase;
 5 None.gif
 6 None.gif import  java.util.List;
 7 None.gif
 8 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
 9InBlock.gif * @author martin.xus
10ExpandedBlockEnd.gif */

11 ExpandedBlockStart.gifContractedBlock.gif public   class  UserTest  extends  MockObjectTestCase  dot.gif {
12InBlock.gif
13ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testAdd() dot.gif{
14InBlock.gif        Mock mock = mock(UserManager.class);
15InBlock.gif        User user = new User();
16InBlock.gif        mock.expects(once()).method("add").with(isA(User.class));
17InBlock.gif        UserManager manager = (UserManager) mock.proxy();
18InBlock.gif
19InBlock.gif        user.setManager(manager);
20InBlock.gif        user.add(new User());
21InBlock.gif        mock.verify();
22ExpandedSubBlockEnd.gif    }

23InBlock.gif
24ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testLoadByName() dot.gif{
25InBlock.gif        Mock mock = mock(UserManager.class);
26InBlock.gif        User user = new User();
27InBlock.gif
28InBlock.gif        mock.expects(once()).method("loadByName").with(isA(String.class)).will(returnValue(List.class));
29InBlock.gif        UserManager manager = (UserManager) mock.proxy();
30InBlock.gif
31InBlock.gif        user.setManager(manager);
32InBlock.gif        user.loadByName("martin");
33InBlock.gif        mock.verify();
34ExpandedSubBlockEnd.gif    }

35InBlock.gif
36ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testRemove()dot.gif{
37InBlock.gif        Mock mock = mock(UserManager.class);
38InBlock.gif        User user = new User();
39InBlock.gif
40InBlock.gif        mock.expects(once()).method("remove").with(isA(User.class));
41InBlock.gif        UserManager manager = (UserManager) mock.proxy();
42InBlock.gif
43InBlock.gif        user.setManager(manager);
44InBlock.gif        user.remove(user);
45InBlock.gif        mock.verify();   
46ExpandedSubBlockEnd.gif    }

47ExpandedBlockEnd.gif}

ok,测试通过