Overview
the concept of mock
Beyond the world of software development, the term "mock" refers to imitation or emulation. Therefore, "mock" can be understood as a surrogate, a substitute. The reference to "mock" in software development is usually understood as a mock object or fake.
Mock objects are substitutes that are used as real objects during debugging.
A mock test is a mock test that is used to replace the test with a dummy object for objects that are not easily constructed during the test. Mockito Frame
Mockito is an open source Java testing Framework based on the MIT Protocol. Mockito distinguishes itself from other simulation frameworks by allowing developers to verify the behavior of the system under test without establishing "expectations". One of the evaluations of mock objects is that the test code of the test system is a high-coupling, because Mockito attempts to remove the pattern of expect-run-verify (desired validation mode) by removing the "desired specification", thus reducing the coupling degree. The result is a simplified test code that makes it easier to read and modify. actual Combat one. Maven dependency
<dependency>
<groupId>org.mockito</groupId>
<artifactid>mockito-all</ artifactid>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12 </version>
<scope>test</scope>
</dependency>
Mockito requires JUnit to work together. second, the test steps
User.java
Package com.ricky.codelab.mockito.domain;
public class User {
private long ID;
private String name;
private String Email;
Public long GetId () {
return ID;
}
public void SetId (long id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getemail () {
return email;
}
public void Setemail (String email) {
This.email = email;
}
@Override public
String toString () {
return "User [id=" + ID + ", name=" + name + ", email=" + email + "]";
}
}
Interface Userdao.java
Package Com.ricky.codelab.mockito.dao;
Import Com.ricky.codelab.mockito.domain.User;
Public interface Userdao {public
User Queryuserbyid (long id);
public int update (user user);
UserService. java
Package Com.ricky.codelab.mockito;
Import Com.ricky.codelab.mockito.dao.UserDao;
Import Com.ricky.codelab.mockito.domain.User;
public class UserService {
private Userdao Userdao;
Public UserService (Userdao Userdao) {
This.userdao = Userdao;
}
Public boolean update (long ID, String name) {
User user = Userdao.queryuserbyid (ID);
if (user==null) {
return false;
}
User.setname (name);
Userdao.update (user);
return true;
}
}
Test class Userservicetest.java
Package Com.ricky.codelab.demo;
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.mockito.ArgumentCaptor;
Import Org.mockito.Mock;
Import org.mockito.MockitoAnnotations;
Import static org.mockito.mockito.*;
Import Com.ricky.codelab.mockito.UserService;
Import Com.ricky.codelab.mockito.dao.UserDao;
Import Com.ricky.codelab.mockito.domain.User;
public class Userservicetest {private UserService userservice;
@Mock private Userdao Userdao;
@Before public void SetUp () {mockitoannotations.initmocks (this);
UserService = new UserService (Userdao);
} @Test public void Testverify () {User user = new User ();
User.setid (1);
User.setname ("Ricky");
When (Userdao.queryuserbyid (1)). Thenreturn (user);
Boolean success = Userservice.update (1, "Jack");
Assert.assertequals (True, success);
Verify (Userdao). Queryuserbyid (1); Argumentcaptor<user> PersoncAptor = Argumentcaptor.forclass (User.class);
Verify (Userdao). Update (Personcaptor.capture ());
User Updateduser = Personcaptor.getvalue ();
Assert.assertequals ("Jack", Updateduser.getname ());
}
}
References
http://mockito.org/