[INFO ] Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
[INFO ] Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
[INFO ] Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
[INFO ] Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@234bef66, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@737996a0, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@61dc03ce, org.springframework.test.context.support.DirtiesContextTestExecutionListener@50f8360d]
代码如下:
package com.saikul.tracker.test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import sun.rmi.runtime.Log;
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath*:springmvc-servlet.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSaikulTracker {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
public void whenfetchData() throws Exception{
String content = "[{'l':false}]";
mockMvc.perform(MockMvcRequestBuilders.post("/api/updateParam").contentType(MediaType.APPLICATION_JSON_UTF8).content(content))
.andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void testJunitRunGood(){
System.out.println("hello world ");
}
}
基本上就是一个没有前端页面的,只提供服务的SSH项目,我加了两个jar 一个Junit的一个Spring test的,想要模拟mvc的访问restful服务