单元测试

master
shijie 4 years ago
parent d62d3df997
commit 251da427b7
  1. 6
      pom.xml
  2. 2
      src/main/java/com/yipin/liuwanr/controller/AliyunOssController.java
  3. 9
      src/main/java/com/yipin/liuwanr/entity/City.java
  4. 9
      src/main/java/com/yipin/liuwanr/entity/SutdentClass.java
  5. 3
      src/main/java/com/yipin/liuwanr/service/AliyunOssService.java
  6. 1
      src/main/java/com/yipin/liuwanr/service/CityService.java
  7. 70
      src/test/java/com/yipin/liuwanr/AliyunOssServiceTest.java
  8. 54
      src/test/java/com/yipin/liuwanr/CityServiceTest.java
  9. 40
      src/test/java/com/yipin/liuwanr/ClassServiceTest.java

@ -232,6 +232,12 @@
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.7.21</version> <version>1.7.21</version>
</dependency> </dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>ROOT</finalName> <finalName>ROOT</finalName>

@ -24,6 +24,7 @@ public class AliyunOssController {
/** /**
* 上传文件 * 上传文件
*
* @param file * @param file
* @return * @return
*/ */
@ -45,6 +46,7 @@ public class AliyunOssController {
/** /**
* 下载文件 * 下载文件
*
* @param objectName在阿里云的文件路径 * @param objectName在阿里云的文件路径
* @param fileurl:本地路径 * @param fileurl:本地路径
* @return * @return

@ -26,4 +26,13 @@ public class City {
public void setProvinceId(Integer provinceId) { public void setProvinceId(Integer provinceId) {
this.provinceId = provinceId; this.provinceId = provinceId;
} }
@Override
public String toString() {
return "City{" +
"cityId=" + cityId +
", cityName='" + cityName + '\'' +
", provinceId=" + provinceId +
'}';
}
} }

@ -36,5 +36,12 @@ public class SutdentClass {
this.gradeId = gradeId; this.gradeId = gradeId;
} }
@Override
public String toString() {
return "SutdentClass{" +
"classId=" + classId +
", className='" + className + '\'' +
", gradeId=" + gradeId +
'}';
}
} }

@ -29,6 +29,7 @@ public class AliyunOssService {
/** /**
* 上传文件 * 上传文件
*
* @param file 上传文件 * @param file 上传文件
* @return * @return
*/ */
@ -51,6 +52,7 @@ public class AliyunOssService {
/** /**
* 下载文件 * 下载文件
*
* @param objectName 要下载的文件(路径+文件名) * @param objectName 要下载的文件(路径+文件名)
* @param fileurl 本地路径 * @param fileurl 本地路径
* @return * @return
@ -71,6 +73,7 @@ public class AliyunOssService {
/** /**
* 根据文件路径+文件名称删除该文件 * 根据文件路径+文件名称删除该文件
*
* @param filename * @param filename
* @return * @return
*/ */

@ -24,6 +24,7 @@ public class CityService {
resp.put("retcode", 200); resp.put("retcode", 200);
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
System.out.println(e.getMessage());
resp.put("retcode", 500); resp.put("retcode", 500);
resp.put("retvalue", "Inquiry Failed"); resp.put("retvalue", "Inquiry Failed");
return resp; return resp;

@ -0,0 +1,70 @@
package com.yipin.liuwanr;
import com.yipin.liuwanr.service.AliyunOssService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AliyunOssServiceTest {
@Autowired
private AliyunOssService aliyunOssService;
/**
* 测试上传文件
*/
@Test
public void testUploadFiles() throws IOException {
File file = new File("D:\\pic\\1.jpg");
FileItem fileItem = new DiskFileItem("file",
Files.probeContentType(file.toPath()),
false,
file.getName(),
(int) file.length(),
file.getParentFile());
IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream());
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
HashMap<String, Object> map = aliyunOssService.uploadFiles(multipartFile);
map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString()));
}
/**
* 测试下载文件
*/
@Test
public void testDownloadFiles() throws IOException {
HttpServletResponse response = new MockHttpServletResponse();
AliyunOssService aliyunOssService = this.aliyunOssService;
aliyunOssService.downloadFiles(response,
"http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg");
}
/**
* 测试根据文件路径+文件名称删除该文件
*/
@Test
public void testDeleteFile() throws IOException {
aliyunOssService.deleteFile("http://liuwanr.oss-cn-shenzhen.aliyuncs.com/jpg/20200807/1596787474773.jpg");
}
}

@ -1,14 +1,17 @@
package com.yipin.liuwanr; package com.yipin.liuwanr;
import com.yipin.liuwanr.entity.City; import com.yipin.liuwanr.entity.City;
import com.yipin.liuwanr.mapper.CityMapper;
import com.yipin.liuwanr.service.CityService; import com.yipin.liuwanr.service.CityService;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ -17,13 +20,62 @@ public class CityServiceTest {
@Autowired @Autowired
private CityService cityService; private CityService cityService;
@Autowired
private CityMapper cityMapper;
/** /**
* 新增城市信息方法测试 * 新增城市信息方法测试
*/ */
@Test /*@Test
@Transactional
public void testAddCity() { public void testAddCity() {
City city = new City(); City city = new City();
city.setCityName("hhhh");
city.setProvinceId(2);
HashMap<String, Object> map = cityService.addCity(city); HashMap<String, Object> map = cityService.addCity(city);
map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value)); map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value));
}*/
/**
* 查询城市信息方法测试
*/
@Test
public void testQueryCity() {
HashMap<String, Object> map = cityService.queryCity(1);
Integer retcode = (Integer) map.get("retcode");
System.out.println(retcode);
if (retcode == 200) {
List<City> cities = (List<City>) map.get("retvalue");
cities.forEach(item -> {
System.out.println(item.toString());
});
} else {
String msg = (String) map.get("retvalue");
System.out.println(msg);
}
}
/**
* 删除城市信息方法测试
*/
@Test
@Transactional
public void testDeleteCity() {
HashMap<String, Object> map = cityService.deleteCity(1);
map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString()));
}
/**
* 删除城市信息方法测试
*/
@Test
@Transactional
public void testUpdateCity() {
City city = new City();
city.setCityId(1);
city.setCityName("hhhh");
city.setProvinceId(2);
HashMap<String, Object> map = cityService.updateCity(city);
map.forEach((key, value) -> System.out.println("key = " + key + " ===> value = " + value.toString()));
} }
} }

@ -0,0 +1,40 @@
package com.yipin.liuwanr;
import com.yipin.liuwanr.entity.City;
import com.yipin.liuwanr.entity.SutdentClass;
import com.yipin.liuwanr.mapper.CityMapper;
import com.yipin.liuwanr.service.CityService;
import com.yipin.liuwanr.service.ClassService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ClassServiceTest {
@Autowired
private ClassService classService;
@Test
public void testQueryGetByClassName() {
HashMap<String, Object> map = classService.queryGetByClassName("");
Integer retcode = (Integer) map.get("retcode");
System.out.println(retcode);
if (retcode == 200) {
List<SutdentClass> classes = (List<SutdentClass>) map.get("retvalue");
classes.forEach(item -> {
System.out.println(item.toString());
});
} else {
String msg = (String) map.get("retvalue");
System.out.println(msg);
}
}
}
Loading…
Cancel
Save