maven中使用spring的test包结合junit4进行测试。
2015-08-04 17:39
639 查看
1.先在src/test/resources中加入spring的配置文件spring.xml
2.引入maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
3.进行测试。
package junit.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.southgis.ibase.file.entity.FileInfo;
import com.southgis.ibase.file.service.FileInfoServiceInterface;
import com.southgis.ibase.file.service.FileServiceInterface;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class FileServiceTest extends AbstractJUnit4SpringContextTests
{
@Resource
FileServiceInterface fileService;
@Resource
FileInfoServiceInterface fileInfoService;
// 把fileService下的uploadTest.txt 上传到fileWebService的test下。
// %WEBSVR%在是根据webRealPath得出的,而webRealPath在src/test/resources/spring.xml中定义了
@Test
public void websvrUploadTest() throws Exception
{
// %WEBSVR%
try
{
File file = new File("uploadTest.txt");
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%WEBSVR%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream(file);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
// %%ftpupload%
}
//把fileService下的uploadTest.txt 上传到fileWebService的appdata/test下。
@Test
public void apppathUploadTest() throws Exception
{
// %APPPATH%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%APPPATH%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//把fileService下的uploadTest.txt 上传到fileWebService的D://upload/abc/test下。
@Test
public void abcUploadTest() throws Exception
{
// %%abc%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%%abc%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//把fileService下的uploadTest.txt 上传到ServerWeb。
//ServerWeb会把文件保存到D:/upload/frj/
//注意:必须先开启ServerWeb服务。
//@Test
public void httpUploadTest() throws Exception
{
// %%httpupload%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%%httpupload%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
// %WEBSVR% %APPPATH% 由spring.xml的webRealPath定义
// %%abc%由spring的propertiesPath定义。
@Test
public void mapUriTest() throws IOException
{
String path1 = fileService.mapUri(false, "%WEBSVR%/test/abc.doc");
String path2 = fileService.mapUri(false, "%APPPATH%/test/abc.doc");
String path3 = fileService.mapUri(false, "%%abc%/test/abc.doc");
FileInfo fileInfo = new FileInfo();
fileInfo.setId("c");
fileInfo.setFileName("test");
fileInfo.setFilePath("%WEBSVR%/test/abc.txt");
fileInfoService.saveOlnyOne(fileInfo);
String path4 = fileService.mapUri(true, "c");
System.out.println("path1:fileWebService/" + path1);
System.out.println("path2:fileWebService/" + path2);
System.out.println("path3:fileWebService/" + path3);
System.out.println("path5:fileWebService/" + path4);
}
//
@Test
public void downlaodTest() throws Exception
{
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setId("e");
fileInfo.setFileName("test");
fileInfo.setFilePath("%WEBSVR%/test/a.jpg");
fileInfoService.saveOlnyOne(fileInfo);
File file1 = new File("src/test/resources/download/a(download-1).jpg");
File file2 = new File("src/test/resources/download/b(download-2).doc");
File file3 = new File("src/test/resources/download/c(download-3).doc");
File file4 = new File("src/test/resources/download/d(download-4).jpg");
createFile(file1);
createFile(file2);
createFile(file3);
createFile(file4);
OutputStream outputStream1 = new FileOutputStream(file1);
OutputStream outputStream2 = new FileOutputStream(file2);
OutputStream outputStream3 = new FileOutputStream(file3);
OutputStream outputStream4 = new FileOutputStream(file4);
System.out.println("---------------download 1-----------");
fileService.download(false, "%WEBSVR%/test/a.jpg", outputStream1);
System.out.println("------------------------------------");
System.out.println("---------------download 2-----------");
fileService.download(false, "%APPPATH%/test/abc.doc", outputStream2);
System.out.println("------------------------------------");
System.out.println("---------------download 3-----------");
fileService.download(false, "%%abc%/test/abc.doc", outputStream3);
System.out.println("------------------------------------");
System.out.println("---------------download 4-----------");
fileService.download(true, "e", outputStream4);
System.out.println("------------------------------------");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
private void createFile(File file) throws IOException
{
if (!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
if (!file.exists())
{
file.createNewFile();
}
}
@Test
public void Test()
{
String filePath = "xxxx/xxx/xxx/asd.txt";
int i = filePath.lastIndexOf(".");
int j = filePath.lastIndexOf("/");
System.out.println(i);
System.out.println(j);
System.out.println(filePath.substring(j, i));
File file1 = new File("D:\\upload\\abc\\frj\\asd.txt");
File file2 = new File("D:/upload/abc/frj/asd.txt");
System.out.println(file1.exists());
System.out.println(file2.exists());
}
}
2.引入maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
3.进行测试。
package junit.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.southgis.ibase.file.entity.FileInfo;
import com.southgis.ibase.file.service.FileInfoServiceInterface;
import com.southgis.ibase.file.service.FileServiceInterface;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class FileServiceTest extends AbstractJUnit4SpringContextTests
{
@Resource
FileServiceInterface fileService;
@Resource
FileInfoServiceInterface fileInfoService;
// 把fileService下的uploadTest.txt 上传到fileWebService的test下。
// %WEBSVR%在是根据webRealPath得出的,而webRealPath在src/test/resources/spring.xml中定义了
@Test
public void websvrUploadTest() throws Exception
{
// %WEBSVR%
try
{
File file = new File("uploadTest.txt");
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%WEBSVR%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream(file);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
// %%ftpupload%
}
//把fileService下的uploadTest.txt 上传到fileWebService的appdata/test下。
@Test
public void apppathUploadTest() throws Exception
{
// %APPPATH%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%APPPATH%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//把fileService下的uploadTest.txt 上传到fileWebService的D://upload/abc/test下。
@Test
public void abcUploadTest() throws Exception
{
// %%abc%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%%abc%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
//把fileService下的uploadTest.txt 上传到ServerWeb。
//ServerWeb会把文件保存到D:/upload/frj/
//注意:必须先开启ServerWeb服务。
//@Test
public void httpUploadTest() throws Exception
{
// %%httpupload%
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setFilePath("%%httpupload%/test/uploadTest.txt");
FileInputStream fileInputStream = null;
try
{
fileInputStream = new FileInputStream("uploadTest.txt");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
throw e;
}
String json = fileService.upload(fileInfo, fileInputStream);
Assert.assertTrue(json.contains("true"));
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
// %WEBSVR% %APPPATH% 由spring.xml的webRealPath定义
// %%abc%由spring的propertiesPath定义。
@Test
public void mapUriTest() throws IOException
{
String path1 = fileService.mapUri(false, "%WEBSVR%/test/abc.doc");
String path2 = fileService.mapUri(false, "%APPPATH%/test/abc.doc");
String path3 = fileService.mapUri(false, "%%abc%/test/abc.doc");
FileInfo fileInfo = new FileInfo();
fileInfo.setId("c");
fileInfo.setFileName("test");
fileInfo.setFilePath("%WEBSVR%/test/abc.txt");
fileInfoService.saveOlnyOne(fileInfo);
String path4 = fileService.mapUri(true, "c");
System.out.println("path1:fileWebService/" + path1);
System.out.println("path2:fileWebService/" + path2);
System.out.println("path3:fileWebService/" + path3);
System.out.println("path5:fileWebService/" + path4);
}
//
@Test
public void downlaodTest() throws Exception
{
try
{
FileInfo fileInfo = new FileInfo();
fileInfo.setId("e");
fileInfo.setFileName("test");
fileInfo.setFilePath("%WEBSVR%/test/a.jpg");
fileInfoService.saveOlnyOne(fileInfo);
File file1 = new File("src/test/resources/download/a(download-1).jpg");
File file2 = new File("src/test/resources/download/b(download-2).doc");
File file3 = new File("src/test/resources/download/c(download-3).doc");
File file4 = new File("src/test/resources/download/d(download-4).jpg");
createFile(file1);
createFile(file2);
createFile(file3);
createFile(file4);
OutputStream outputStream1 = new FileOutputStream(file1);
OutputStream outputStream2 = new FileOutputStream(file2);
OutputStream outputStream3 = new FileOutputStream(file3);
OutputStream outputStream4 = new FileOutputStream(file4);
System.out.println("---------------download 1-----------");
fileService.download(false, "%WEBSVR%/test/a.jpg", outputStream1);
System.out.println("------------------------------------");
System.out.println("---------------download 2-----------");
fileService.download(false, "%APPPATH%/test/abc.doc", outputStream2);
System.out.println("------------------------------------");
System.out.println("---------------download 3-----------");
fileService.download(false, "%%abc%/test/abc.doc", outputStream3);
System.out.println("------------------------------------");
System.out.println("---------------download 4-----------");
fileService.download(true, "e", outputStream4);
System.out.println("------------------------------------");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
private void createFile(File file) throws IOException
{
if (!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
if (!file.exists())
{
file.createNewFile();
}
}
@Test
public void Test()
{
String filePath = "xxxx/xxx/xxx/asd.txt";
int i = filePath.lastIndexOf(".");
int j = filePath.lastIndexOf("/");
System.out.println(i);
System.out.println(j);
System.out.println(filePath.substring(j, i));
File file1 = new File("D:\\upload\\abc\\frj\\asd.txt");
File file2 = new File("D:/upload/abc/frj/asd.txt");
System.out.println(file1.exists());
System.out.println(file2.exists());
}
}
相关文章推荐
- MyBatis Spring整合配置映射接口类与映射xml文件
- Java并发编程之Callable,Future,FutureTask
- Java一处编译到处运行的简单原理
- springMVC接收对象参数
- Java并发编程:深入剖析ThreadLocal
- JAVA 中输入的方法(如何在java中输入)
- SpringMVC jpa 分页问题
- Spring.NET学习笔记
- Java的HashMap和HashTable
- Java编程风格与命名规范整理
- Java内存与垃圾回收调优
- Eclipse 实现关键字自动补全功能
- php_xdebug安装+NetBeans的配置和使用
- java笔记(三)[java中的继承&java中的方法重载,重写,重构&java中的向上转型和乡下转型&java中的抽象类和接口&java中的接口向上转型]
- java String功能--13
- java常用设计模式
- Java读取properties文件中文乱码问题解决
- springmvc:406
- java异常处理
- java反射详解