Junit单元测试指南
必要条件
相关配置
位置
/shop-core/src/test/resources/config将这里面的相关配置配置好,尤其是数据库的配置
必须继承SpringTestSupport
public class GoodsQueryManagerTest extends SpringTestSupport { ...DEMO
- 业务类测试
@Autowired
private IGoodsQueryManager goodsQueryManager;
@Test
public void testAdd() throws Exception {
Integer[] goodsidAr =new Integer[] {1,2};
List goodsList =goodsQueryManager.query(goodsidAr);
//做相应的断言
}
Controller测试
简单参数式
@Test public void testAdd() throws Exception { mockMvc.perform( MockMvcRequestBuilders.get("/xxx.do").param("name", "value") ); }多参数式
@Test public void testAdd() throws Exception { MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("name", "wf"); params.add("age","35"); mockMvc.pJson式
@Test public void testAdd() throws Exception { String json ="{ xxx }"; mockMvc.perform( MockMvcRequestBuilders.get("/xxx.do").contentType(MediaType.APPLICATION_JSON_UTF8).content(json)); }规范
- 位置
必须在/src/test/java 中 - 包名
和测试的业务类包名相同 - 类名
必须以 <测试目标类>Test.java来命名 如:GoodsControllerTest