单元测试

master
shijie 4 years ago
parent d62d3df997
commit 251da427b7
  1. 6
      pom.xml
  2. 6
      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. 19
      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>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ROOT</finalName>

@ -24,6 +24,7 @@ public class AliyunOssController {
/**
* 上传文件
*
* @param file
* @return
*/
@ -45,15 +46,16 @@ public class AliyunOssController {
/**
* 下载文件
*
* @param objectName在阿里云的文件路径
* @param fileurl:本地路径
* @return
*/
@GetMapping("/downloadFiles")
Response downloadFiles(HttpServletResponse response,@RequestParam String objectName) {
Response downloadFiles(HttpServletResponse response, @RequestParam String objectName) {
Response resp = new Response();
try {
ossService.downloadFiles(response,objectName);
ossService.downloadFiles(response, objectName);
resp.setStatus(200);
resp.setMessage("下载成功!");
} catch (Exception e) {

@ -26,4 +26,13 @@ public class City {
public void setProvinceId(Integer 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;
}
@Override
public String toString() {
return "SutdentClass{" +
"classId=" + classId +
", className='" + className + '\'' +
", gradeId=" + gradeId +
'}';
}
}

@ -29,14 +29,15 @@ public class AliyunOssService {
/**
* 上传文件
*
* @param file 上传文件
* @return
*/
public HashMap<String, Object> uploadFiles(MultipartFile file) {
HashMap<String, Object> resp = new HashMap<String, Object>();
try {
OSSClient client=beanFactory.getBean(OSSClient.class);
FilesResult result=OssConfigUtil.uploadFiles(file, client, config);
OSSClient client = beanFactory.getBean(OSSClient.class);
FilesResult result = OssConfigUtil.uploadFiles(file, client, config);
resp.put("retcode", 200);
resp.put("retvalue", result);
} catch (Exception e) {
@ -51,17 +52,18 @@ public class AliyunOssService {
/**
* 下载文件
*
* @param objectName 要下载的文件(路径+文件名)
* @param fileurl 本地路径
* @return
*/
public void downloadFiles(HttpServletResponse response,String objectName) {
public void downloadFiles(HttpServletResponse response, String objectName) {
HashMap<String, Object> resp = new HashMap<String, Object>();
try {
OSSClient client=beanFactory.getBean(OSSClient.class);
OssConfigUtil.downloadFiles(response,client, config, objectName);
OSSClient client = beanFactory.getBean(OSSClient.class);
OssConfigUtil.downloadFiles(response, client, config, objectName);
resp.put("retcode", 200);
resp.put("retvalue","Download SUCCESS" );
resp.put("retvalue", "Download SUCCESS");
} catch (Exception e) {
logger.info(e.getMessage());
resp.put("retcode", 500);
@ -71,16 +73,17 @@ public class AliyunOssService {
/**
* 根据文件路径+文件名称删除该文件
*
* @param filename
* @return
*/
public HashMap<String, Object> deleteFile(String filename) {
HashMap<String, Object> resp = new HashMap<String, Object>();
try {
OSSClient client=beanFactory.getBean(OSSClient.class);
OSSClient client = beanFactory.getBean(OSSClient.class);
OssConfigUtil.deleteFile(client, config.getBucketName(), filename);
resp.put("retcode", 200);
resp.put("retvalue","Delete SUCCESS" );
resp.put("retvalue", "Delete SUCCESS");
} catch (RuntimeException e) {
logger.info(e.getMessage());
resp.put("retcode", 500);

@ -24,6 +24,7 @@ public class CityService {
resp.put("retcode", 200);
} catch (RuntimeException e) {
logger.error(e.getMessage());
System.out.println(e.getMessage());
resp.put("retcode", 500);
resp.put("retvalue", "Inquiry Failed");
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;
import com.yipin.liuwanr.entity.City;
import com.yipin.liuwanr.mapper.CityMapper;
import com.yipin.liuwanr.service.CityService;
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
@ -17,13 +20,62 @@ public class CityServiceTest {
@Autowired
private CityService cityService;
@Autowired
private CityMapper cityMapper;
/**
* 新增城市信息方法测试
*/
@Test
/*@Test
@Transactional
public void testAddCity() {
City city = new City();
city.setCityName("hhhh");
city.setProvinceId(2);
HashMap<String, Object> map = cityService.addCity(city);
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