其他分享
首页 > 其他分享> > mockito 学习

mockito 学习

作者:互联网

 

相关对等操作
code anotation note
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@RunWith(MockitoJUnitRunner.class)
public class MockitoAnnotationTest {
...
}
用于启用mockito
List mockList = Mockito.mock(ArrayList.class); @Mock
List<String> mockedList;
mock出的对象,并不会真正执行对应的方法
List<String> spyList = Mockito.spy(new ArrayList<String>()); @Spy
List<String> spiedList = new ArrayList<String>();
spy出的对象,是会真正执行的
ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class); @Captor
ArgumentCaptor argCaptor;
@Mock
List mockedList;
@Captor
ArgumentCaptor argCaptor;
@Test
public void whenUseCaptorAnnotation_thenTheSam() {
mockedList.add("one");
Mockito.verify(mockedList).add(argCaptor.capture());
assertEquals("one", argCaptor.getValue());
}

 

@InjectMocks的用法

@Repository
public class StatusDAO{
    @Autowired private CosmosClient client;
    
    public void markApproved(Document doc) {
        client.executeStoredProcedure(getCollectionURI(), "markApproved", doc);
    }
}

@RunWith(MockitoJUnitRunner.class)
public class StatusDAOTest{

    @Spy
    @InjectMocks
    StatusDAO statusDAO;

    @Mock
    CosmosClient client = new CosmosClient();
    @Mock
    Document document;
    @Spy
    StatusDocument statusDocument = new StatusDocument();

    @Before
    public void setup() {
        Mockito.doReturn(document).when(client).executeStoredProcedure(Mockito.anyString(), Mockito.anyString(),Mockito.any(StatusDocument.class));
    }
}

 

 

c

 

标签:Mockito,mockedList,ArrayList,mockito,class,学习,new,public
来源: https://www.cnblogs.com/cynrjy/p/15681454.html