1.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package com.tome.testService;
import javax.annotation.Resource;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.BeforeMethod;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import com.google.common.collect.Maps;
import com.tome.model.HeartRate;
import com.tome.service.HeartRateService;
import com.tome.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)//让junit工作在spring环境中
@ContextConfiguration({"classpath:spring.xml","classpath:spring-mvc.xml"})//在classes中spring的配置文件
//transactionManager表示在spring配置文件中所声明的事务对象mvn install -Dmaven.test.skip=true
//defaultRollback=true表示操作会自动回滚,这样你在单元测试中所作的操作都不会影响数据库中的数据
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
@WebAppConfiguration
public class TestController extends AbstractTestNGSpringContextTests
{
@Autowired
private WebApplicationContext wac;//WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletContext);
private MockMvc mockMvc;
@BeforeMethod
public void setUp() {
mockMvc =MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@org.testng.annotations.Test
public void accessTomeController(String path, String value) throws Exception {
/**
*
* this.mockMvc.perform(get("/tome/testHello")
.accept(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andExpect(view().name("helloworld"))
.andExpect(MockMvcResultMatchers.model().attribute("user", "zhuyuping"));
*
*
*
* this.mockMvc.perform(get("/json/testJson")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.user").value("朱遇平"));
*
*
*
*
*/
mockMvc.perform(get("/ckset")).andExpect(status().isOk()); //这是测试没有返回结果的方法测试
}
}
|
2.
@WebAppConfiguration @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:config/applicationContext.xml","classpath:config/applicationContext-jpa.xml","classpath:config/spring-mvc.xml"}) public class LiquidationProjectTypeRulesTemplateServiceTest { @Autowired private LiquidationProjectTypeRulesTemplateService service; @Test public void testAddLiquidationProjectTypeRulesTemplate() { LiquidationProjectTypePojo pojo = new LiquidationProjectTypePojo(); // pojo.setProjectId(1L); pojo.setProjectTypeId(1L); pojo.setProjectRatio(10); pojo.setPersonnalRatio(10); pojo.setSmallThreshold1("1"); // pojo.setBigThreshold1(3f); pojo.setRatio1(1); // // pojo.setSmallThreshold2(4f); // pojo.setBigThreshold2(10f); // pojo.setRatio2(20f); // // pojo.setSmallThreshold3(11f); // pojo.setBigThreshold3(13f); // pojo.setRatio3(30f); List<LiquidationProjectTypePojo> list = new ArrayList<LiquidationProjectTypePojo>(); list.add(pojo); //service.addLiquidationProjectTypeRulesTemplate(list); } @Test public void query(){ // service.getLiquidationProjectType(); try { service.getLiquidationProjectData(); } catch (HossException e) { // TODO Auto-generated catch block e.printStackTrace(); } //System.out.println(Jsonp.success(list)); } }