您的位置:首页 > 编程语言 > Java开发

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());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: