???????????????????????ó????????????????????????????????????JUnit????????????????????????????????????????????????????????Spring?????????????????????JUnit??????????????????????JUnit???????Spring??????????????????????????????????????????????о??????????????????????????????????????????????С?Mockito??Unitils??Dbunit???????????Щ????????????????????????Unitils???Dbunit?????DAO???????????????????????д??????????Ч?????????

?????????????????????testNG??JUnit?????????????????????????????????????????????????Mockito???????????JMock???????????

?????????????

?????????Java?????Mock?????????EasyMock??JMock??Mockito??MockCreator??Mockrunner??MockMaker???Mockito????????Java??Mocking????????EasyMock??JMock???????????????????????????????????????? Mock ???????????????????дMock??????Mockito?????????к?У????????????????????????????????Expectations????????????Mockito????????????軨???????????????ü???API??д????????????????????????Mock?????????С????????Mock???????????

????Mockito??????????????????????????з??????API??????????????????????????????????????????????????????Mockito????????????й?Mockito?????????????????????http://www.mockito.org/?????????Mockito?????????????Stub??Mock?????????Easymock??JMock????д????????????????????????????mock???????÷???????????????????????μ?汾??1.9.0??

????Stub????????????????????????????????????????????????????????????????????÷???????????????Mockito?? when(??).thenReturn(??) ???????????????÷??????????????????????????÷????????????????????

????Mock????????????????????????????????????????????Mockito???? verify(??).methodXxx(??) ??????? methodXxx???????????????????á??й? stub??mock????????????Martin Fowler???????Mocks Aren??t Stub????????http://martinfowler.com/articles/mocksArentStubs.html????Mocking???????ν??Mock????????????????????Stub??Mock????????????????????????÷??????÷??????????????????????á?

????????Mock????

?????????????????Mock???????????????????????Mock?????????????????????????????Mock?????????????????????????????磬????Mock????????????????????л?????????Mock?????????????????????????????????λ???????????Mock??????????????????????????????final?????????Java??????????????????Mock?????????Mock??????????????????mock??Class classToMock????????????@mock?????Mock???????????????????????????δ??????Mock????

????Java????

import org.junit.Test;
import org.mockito.Mock;
import com.baobaotao.domain.User;
import com.baobaotao.service.UserService;
import com.baobaotao.service.UserServiceImpl;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.mockito.MockitoAnnotations;
??
public class MockitoSampleTest{
   
 //?? ??????????
 UserService mockUserService = mock(UserService.class);
 //?? ??????????
 UserServiceImpl mockServiceImpl = mock(UserServiceImpl.class);
//?? ????????????
@Mock
User mockUser;
 
    @Before
    public void initMocks() {
    //?? ??????????????????@Mock?????????
        MockitoAnnotations.initMocks(this);
    }
     ??
 }
??

?????????????????Mockito????mock()????????UserService ??????????????????????UserServiceImpl?????????????????@Mock????????User?????????????????????????????У????MockitoAnnotations.initMocks()???????????????????????д???@Mock??????????????????????????????????????????????????????????
???趨Mock???????????????????

?????????????????????????????when(mock.someMethod()).thenReturn(value)???趨Mock?????????????????????????????????????????????????static??final???ε??????????????趨????????????????????????????ε??÷??????趨???????

$news_Page$

??Java????

import org.junit.Test;
import org.mockito.Mock;
import com.baobaotao.domain.User;
import com.baobaotao.service.UserService;
import com.baobaotao.service.UserServiceImpl;
??
public class MockitoSampleTest {
     ??

     //?? ?????UserService????
 @Test
 public void testMockInterface() {
          //??-1 ??????趨?????
 when(mockUserService.findUserByUserName("tom")).thenReturn(
    new User("tom"?? "1234"));
//??-2 ??????趨?????
doReturn(true).when(mockServiceImpl).hasMatchUser("tom"?? "1234");
 //??-3 ??void???????з???????趨
User u = new User("John"?? "1234");
 doNothing().when(mockUserService).registerUser(u);

//??-4 ??з???????
  User user = mockUserService.findUserByUserName("tom");
  boolean isMatch = mockUserService.hasMatchUser("tom"??"1234");
      mockUserService.registerUser(u);

  assertNotNull(user);
  assertEquals(user.getUserName()?? "tom");
  assertEquals(isMatch?? true);
 }

 //?? ????????UserServiceImpl????
@Test
 public void testMockClass() {
          // ??????趨?????
  when(mockServiceImpl.findUserByUserName("tom"))
.thenReturn(new User("tom"?? "1234"));
 doReturn(true).when(mockServiceImpl).hasMatchUser("tom"?? "1234");

  User user = mockServiceImpl.findUserByUserName("tom");
  boolean isMatch = mockServiceImpl.hasMatchUser("tom"??"1234");
  assertNotNull(user);
  assertEquals(user.getUserName()?? "tom");
  assertEquals(isMatch?? true);
 }
 
 //?? ???User?????
@Test
 public void testMockUser() {
  when(mockUser.getUserId()).thenReturn(1);
  when(mockUser.getUserName()).thenReturn("tom");
  assertEquals(mockUser.getUserId()??1);
  assertEquals(mockUser.getUserName()?? "tom");
 }
??
 
??????????????????UserService??findUserByUserName()??????hasMatchUser()??????registerUser()?????????-1?????when().thenReturn()????????????ü????÷???????????????????????UserService ????????????????findUserByUserName()?????????????????tom?????????????????÷???User????new User("tom"?? "1234")?????-2?????doReturn (). when ()????????ж????hasMatchUser()???????????ж?????????tom???????????1234?????????????????÷???????true?????-3????void???????з???????趨????????е?????????registerUser()???????趨???÷???????????????????н??????????????????????????????????????????UserServiceImpl?????????????????????

??????????????

????Mock????????????????????????????????????????????????????佻????????????????Mockito?????mock????????????????verify(mock). xxx()?????????????????findUserByUserName()??????????????????????Σ???????verify???????????times??????atLeastOnce()????????????????????????????

????Java????

import org.junit.Test;
import org.mockito.Mock;
import com.baobaotao.domain.User;
import com.baobaotao.service.UserService;
import com.baobaotao.service.UserServiceImpl;
??
public class MockitoSampleTest {
     ??

     //?? ?????UserService????
 @Test
 public void testMockInterface() {
          ??
 when(mockUserService.findUserByUserName("tom"))
                         .thenReturn(new User("tom"?? "1234"));
User user = mockServiceImpl.findUserByUserName("tom");

//??-4 ????????
  assertNotNull(user);
  assertEquals(user.getUserName()?? "tom");
  assertEquals(isMatch?? true);

 //??-5 ??????????
verify(mockUserService).findUserByUserName("tom");

//??-6 ?????????????????
verify(mockUserService?? atLeastOnce()).findUserByUserName("tom");
verify(mockUserService?? atLeast(1)).findUserByUserName("tom");
 
//??-7 ?????????????????
verify(mockUserService?? atMost(1)).findUserByUserName("tom");
 }
??
 
????Mockio?????????????÷??????????????????類?????????????verify(xxx?? times(x))??????x??verify(xxx?? atLeast (x))????x??verify(xxx?? atMost (x))????δ??????verify(xxx?? never())?????-6???????findUserByUserName()???????????????Ρ????-7???????findUserByUserName()??????????????Ρ?