在
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
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;
/**
* 测试类
*
* @version 1.0.0
* @since 2015年1月12日 下午3:12:57
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
//(value = "zeus-web/src/main/webapp")
@ActiveProfiles("develop")
@TransactionConfiguration(defaultRollback = true, transactionManager = "transactionManager")
@Transactional
@ContextConfiguration(locations = {"classpath:config/dispatcher-servlet.xml", "classpath:config/applicationContext-base.xml", "classpath:config/applicationContext-mailbeans.xml", "classpath:config/applicationContext-redis.xml",
"classpath:config/applicationContext-profile.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
//reset app context
public class BaseTest //extends AbstractTransactionalTestNGSpringContextTests
{
MockMvc mockMvc;
@Autowired
WebApplicationContext wac; // cached
@Autowired
MockServletContext servletContext; // cached
private static final ObjectMapper objectMapper = new ObjectMapper();
public void setUp() throws Exception {
String contextPath = BaseTest.class.getResource("/").toURI().toString();
String prefix = "file:";
contextPath = contextPath.substring(prefix.length());
servletContext.setContextPath(contextPath);
// 初始化mockMvc
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
protected String retreiveActivePath(String content) {
String aPrefix = "<a href=\"";
String aSubfix = "\">";
String apiv1 = "/apiv1";
int start = content.indexOf(aPrefix);
int end = content.indexOf(aSubfix, start + aPrefix.length());
String activeUri = content.substring(start + aPrefix.length(), end);
activeUri = activeUri.substring(activeUri.indexOf(apiv1));
return activeUri;
}
protected static String toJsonString(Object obj) throws JsonProcessingException
{
return objectMapper.writeValueAsString(obj);
}
}
在