maven中使用spring的test包结合junit4进行测试。

本文介绍如何在Maven项目中结合Spring的test包和JUnit4进行单元测试。首先,需要在`src/test/resources`下配置spring.xml文件,然后引入必要的Maven依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值