mockito piling testing framework

UserDaoImpl UserServiceImpl unit test on a test UserServiceImpl or UserServiceDao

During the test, sometimes we want to achieve some of the indicators of coverage, or coverage indicators because if not we will encounter this problem:
Test Method A call to the other methods B, and B method for some reason can not successfully call or we do not want to call the B method.
We need a mechanism for increasing the controllability test, we want to test more liberal part of the test,

mockito is to help us finish the work tool.

There are two main role mockito:


Verify an example of a method is invoked in the test, how many times is called

Skip the contents of a practical implementation example of a method,
a direct result of our set of return

mockito is a unit testing tool, we said at the beginning,
unit testing is to focus on internal methods, testing of the smallest unit in which we might inevitably involve other services such as method calls,
we can use mockito to skip these steps. mock meaning of the word is false, fool,
we replace it with a fake objects to create their own real target to achieve our results, the original class will not have an impact of changes in the original object.


Mock mechanism using the micro-services provided by the framework, consumers can generate analog test piles client and provider service end test piles, respectively, the two sides can be tested based interface contract Mock test piles for micro services, the two sides do not need to wait for the other side function code developed to achieve parallel development and testing, improving the efficiency of micro-services building. The change can quickly find the interface is not compatible with contract-based test interfaces, such as modifying the field type, delete the field and so on.


Mockito.when(mapper.get(0)).thenReturn(user);

// When faced with external resources dependent on external resources piling, simulation. The method to achieve the logical correctness testing purposes,

Piling method, a return to their own defined objects

mockito3个注解的使用:

@InjectMocks: 创建一个实例 即你要单元测试类 userServiceImp


@Mock: 创建一个Mock.   (其他接口,使用@Mock或@Spy注解注入到该实例中userServiceImp) 

注意:必须使用@RunWith(MockitoJUnitRunner.class) 或 Mockito.initMocks(this)进行mocks的初始化和注入
@RunWith(MockitoJUnitRunner.class)
public class UserServiceImplTest {

@InjectMocks 
UserServiceImp userServiceImp;


@Mock
private JdbcTemplate jdbcTemplateMock;

@Mock
UserMapper mapper;  //此mock将被注入到userServiceImp

@Before
public void before(){
  MOCKITOannotations.initMocks(this);
}

@Test
public void testGet(){
    User user = new User();
    user.setName("huaAn");
    user.setId(0);
    user.setAccount("9527");
    Mockito.when(mapper.get(0)).thenReturn(user);


mockito During the test, sometimes we want to achieve some of the coverage index      

1, check whether the method invocation

Mockito provided vertify key verify whether implemented method is called, calls the following specific examples:

@Test
public void update() throws Exception {
     boolean result = personService.update(1, "new name");
     //验证mockDao的getPerson是否被调用
     verify(mockDao).getPerson(1);
}

//运行结果正确,因为我们在执行update时,调用了getPerson

public boolean update(int id, String name) {
        Person person = personDao.getPerson(id);
        if (person == null) {
            return false;
        }
        Person personUpdate = new Person(person.getId(), name);
        return personDao.update(personUpdate);
}

    org.junit.Assert.assertTrue (userServiceImp.getUser (0) .equals (User));
    Mockito.verify (jdbcTemplateMock) .queryForlist (SQL);. // the Mockito omitted here, is a note jdbcTemplateMock dao interface with verify () wrap
   // validate a method jdbcTemplate an invoked object does not throw exception, described verified by

}

Published 78 original articles · won praise 12 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_29883183/article/details/88219307