码迷,mamicode.com
首页 > 编程语言 > 详细

maven项目中jar包中高版本和低版本的问题

时间:2018-09-03 19:27:47      阅读:691      评论:0      收藏:0      [点我收藏+]

标签:数组   import   pom   阿里   例子   ppc   orm   config   测试   

最近在使用一个spring自己封装的MockMvc自己封装的测试框架来测试我写的接口,Mockmvc的最大好处就是不可启动服务器来测试controller程序,下面看一下程序

 

用到这三个注解

  • RunWith(SpringJUnit4ClassRunner.class): 使用Spring Test组件进行单元测试;
  • WebAppConfiguration: 使用这个Annotate会在跑单元测试的时候真实的启一个web服务,然后开始调用Controller的Rest API,待单元测试跑完之后再将web服务停掉;
  • ContextConfiguration: 指定Bean的配置文件信息,可以有多种方式,这个例子使用的是文件路径形式,如果有多个配置文件,可以将括号中的信息配置为一个字符串数组来表示; 

            测试代码如下:

import org.junit.runner.RunWith;
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.MvcResult;
import org.springframework.test.web.servlet.ResultActions;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring-servlet.xml","classpath*:/spring-config.xml"})
@WebAppConfiguration
public class BaseJunitTest {

protected MockMvc mockMvc;
protected ResultActions resultActions;
protected MvcResult mvcResult ;

}

 

 

 

 

@Test
public void sendJMJKK0104Test() {
Map map = new HashedMap();
map.put("app_userID", "254852");
map.put("virtual_card_id", "502C3CA91F65706E6BCDC9941860E980D38C53E34475F4D0CC47122EB3239252");
map.put("qr_code_type", "1"); // 二维码类型
map.put("qr_code_load_type", "1"); // 二维码获取方式
map.put("tranCode", "01");// 健康卡应用业务类别代码

sendPostMvc("访问的路径", map);
}

private String sendPostMvc(String path,Map params) {
try {

ResultActions result = mockMvc.perform(post(path)
.contentType(MediaType.APPLICATION_JSON)//验证响应contentType
.content(JSONObject.toJSONString(params)));//传递json格式参数
result.andDo(print());
result.andExpect(MockMvcResultMatchers.status().isOk());

String responseString = result.andReturn().getResponse().getContentAsString();

System.out.println("=====客户端获得反馈数据:" + responseString);
return responseString;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

代码一切都没问题,问题出来了,就是这个MockMvc对spring版本有要求,而在项目使用的框架是低版本的,刚开始在pom.xml里面把我springde jar包换成4.0以上的。但是呢。项目就是会报各种各样jar版本不兼容的问题,比如这个技术分享图片

就是高版本里面spring没有集成json的jar包(我用的阿里的),后来解决了,又来了一个log4j问题,总之各种高低版本不能互相兼容,然后我就进入pom文件中的jar进入看了一下,

技术分享图片

只要在某一个或者某两个的jar包这种核心的jar包,其他的核心的jar包就会自动引入过来,在maven项目中引入jar包的时候很多事jar本身的依赖就会自动引过来,由于我遇到的版本的问题,就需要手动删除或者增加自己需要的jar包的依赖,然后才能解决,如图所示中我需要的一些基本jar就可以直接使用spring-security-config自动引过来就行了。

maven项目中jar包中高版本和低版本的问题

标签:数组   import   pom   阿里   例子   ppc   orm   config   测试   

原文地址:https://www.cnblogs.com/evildoer-bugs/p/9580037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!