C/C++教程

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 学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!