Overall goal: To achieve the purpose of automated test interface
Project composition: Spring MVC + hibernate + MySQL
How do I use mocks for unit testing of interfaces?
Implementation ideas: Replace MySQL with H2 database, before hibernate datesource configuration is MySQL, now configured as H2, so the test database is clean, because in memory. Clear the In-memory database before each JUnit mock test
Implementation code:
Package Cn.edu.hebtu.www.onemeet.client.controller;
Import Cn.edu.hebtu.www.onemeet.client.model.Company;
Import cn.edu.hebtu.www.onemeet.client.model.Department;
Import Cn.edu.hebtu.www.onemeet.client.model.Me;
Import cn.edu.hebtu.www.onemeet.common.model.Reply;
Import Cn.edu.hebtu.www.onemeet.common.web.filter.EncodeFilter;
Import Cn.edu.hebtu.www.onemeet.utils.TestUtils;
Import Org.apache.log4j.Logger;
Import Org.junit.Before;
Import Org.junit.FixMethodOrder;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import Org.junit.runners.MethodSorters;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.core.io.Resource;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import org.springframework.test.context.web.WebAppConfiguration;
Import Org.springframework.test.jdbc.JdbcTestUtils;
Import ORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC;
Import Org.springframework.test.web.servlet.MvcResult;
Import Org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Import Org.springframework.test.web.servlet.setup.MockMvcBuilders;
Import Org.springframework.web.context.WebApplicationContext;
Import static org.junit.Assert.assertEquals;
Import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
Import Javax.sql.DataSource;
Import java.util.List;
/**
* Mock test, using mock test to ensure the correctness of the interface
* Created by chenzhibing on 15-10-30.
*/
@FixMethodOrder (methodsorters.name_ascending)
@RunWith (Springjunit4classrunner.class)
@WebAppConfiguration (value = "Src/main/webapp")
@ContextConfiguration (locations = {"Classpath:spring.xml", "Classpath:spring-hibernate-h2.xml"})
public class Mocktest {
private static Logger Logger = Logger.getlogger (Mocktest.class);
@Autowired
Private Webapplicationcontext WAC;
Private MOCKMVC Mockmvc;
private JdbcTemplate template;
@Autowired
Private Encodefilter Encodefilter;
@Autowired
Private Opensessioninviewfilter Opensessioninviewfilter;
@Autowired
public void Setdatasource (DataSource DataSource) {
Template = new JdbcTemplate (DataSource);
}
@Before
public void SetUp () {
MOCKMVC = Mockmvcbuilders.webappcontextsetup (WAC). Addfilters (This.encodefilter, This.opensessioninviewfilter). Build ();
}
@Before
public void Cleandata () {
String script = "Classpath:H2_Init.sql";
Resource Resource = wac.getresource (script);
Jdbctestutils.executesqlscript (template, resource, true);
}
@Test
public void Hello () throws Exception {
Mvcresult result = Mockmvc.perform (Mockmvcrequestbuilders.post ("/user/hello"))
. Anddo (print ())
. Andreturn ();
String response = Result.getresponse (). getcontentasstring ();
Reply Reply = testutils.getstringreply (response);
Assertequals (Reply.getcode (), 1);
}
@Test
public void Getcompanylist () throws Exception {
Mvcresult result = Mockmvc.perform (Mockmvcrequestbuilders.post ("/user/get_companylist"))
. Anddo (print ())
. Andreturn ();
String response = Result.getresponse (). getcontentasstring ();
reply<list<company>> Reply = testutils.getcompanylistreply (response);
Assertequals (Reply.getcode (), 1);
Assertequals (Reply.getmsg (). Size (), 3);
Assertequals (Reply.getmsg (). Get (0). Getdepartlist (). Size (), 4);
}
}
}
Unit testing of the controller layer of spring MVC using a mock