这么开一个帖子还真没有必要,虽然还是比较高兴.不过还是当做散分帖子吧,100分也不缺少嘛.来信已经收到.你对单元测试的考虑还是简单了.其实比你的考虑要复杂很多.回信就不单写了,俺做了一段关于你的copyFile方法的单元测试的片段.你先看看:
运行需要注意,在classpath中应该有这么一个文件:"/testdata/FileControl_access.test.data",testdata是目录名,文件的内容是什么都无所谓.这个文件用来保证你的单元测试的数据状态.
另外,单元测试为了做到不断重入而每次的运行状态都独立,所以考虑到了临时文件的建立和删除.这样的处理,在每次运行单元测试的时候都无需人工的干涉.
再来看看copyFile方法的测试:断言是建立在此方法的工作内容上的.copyFile的动作是建立一个新的文件(如果目标文件存在是否覆盖视你的方法的规范而定),而且新文件的二进制内容和源文件是必须一致的.这就是你必须要断言的东西.而你原有的方法都没有做到.<<
package view;import junit.framework.TestCase;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;/**
* {@link FileControl_access} test case.
*
* @author Someone
* @version Somerevision
*/
public class TestFileControl_access extends TestCase {
private static final String TEST_DATA = "/testdata/FileControl_access.test.data";
private static final String testDataFile;
private static final String destPath; /**
* Initialize the test data file name at the static initialization block.
* It will search the classpath for test data file specified by {@link #TEST_DATA}.
* @throws RuntimeException If test data file cannot be found.
*/
static {
URL url = TestFileControl_access.class.getResource(TEST_DATA);
if (null == url) {
throw new RuntimeException("Test data file not ready.");
}
testDataFile = url.getFile(); String path = TestFileControl_access.class.getProtectionDomain().getCodeSource().getLocation().getFile();
String absolutePath = new File(path).getAbsolutePath().replace('\\', '/');
destPath = absolutePath.endsWith("/") ? absolutePath : absolutePath + "/";
} /**
* Random object, for random file name generation.
*/
private final Random random = new Random(System.currentTimeMillis());
public TestFileControl_access(String name) {super(name);} /**
* Test copyFile method.
* @see FileControl_access#copyFile(File, File)
* @see FileControl_access#copyFile(String, String)
*/
public void testCopyFile() {
// 1. assert copy correctly
File srcFile = new File(testDataFile);
byte[] srcBytes = readTheSpecifiedFile(srcFile);
File dstFile = new File(generateRandomFileName());
assertFalse(dstFile.exists());
assertTrue(new FileControl_access().copyFile(srcFile, dstFile));
assertTrue(dstFile.exists());
byte[] dstBytes = readTheSpecifiedFile(dstFile);
assertTrue(Arrays.equals(srcBytes, dstBytes));
removeSpecifiedFile(dstFile); // 2. go on others ...
} // private : /**
* Read given file into byte array. And ignore the argument checking.
* If some IO error occurs, just throw out the RuntimeException directly.
* @param source Source file.
* @return byte array of given file.
*/
private byte[] readTheSpecifiedFile(File source) {
try {
FileInputStream fis = new FileInputStream(source);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while ((ch = fis.read()) != -1) {
bos.write(ch);
}
byte[] result = bos.toByteArray();
fis.close();
bos.close();
return result;
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} /**
* Generate random file name (Uppercase characters).
* @return Random file name, never return null.
*/
private String generateRandomFileName() {
final int fileNameLength = 8;
StringBuffer result = new StringBuffer();
for (int i = 0; i < fileNameLength; i++) {
char each = (char) ('A' + random.nextInt(26));
result.append(each);
}
return destPath + result.toString();
} /**
* Delete the given file.
* @param file The file will be erased.
*/
private void removeSpecifiedFile(File file) {
file.delete();
}
}
>>
来信中你提到你刚开始学习测试,其实这些内容都应该是开发人员来做的.:)
运行需要注意,在classpath中应该有这么一个文件:"/testdata/FileControl_access.test.data",testdata是目录名,文件的内容是什么都无所谓.这个文件用来保证你的单元测试的数据状态.
另外,单元测试为了做到不断重入而每次的运行状态都独立,所以考虑到了临时文件的建立和删除.这样的处理,在每次运行单元测试的时候都无需人工的干涉.
再来看看copyFile方法的测试:断言是建立在此方法的工作内容上的.copyFile的动作是建立一个新的文件(如果目标文件存在是否覆盖视你的方法的规范而定),而且新文件的二进制内容和源文件是必须一致的.这就是你必须要断言的东西.而你原有的方法都没有做到.<<
package view;import junit.framework.TestCase;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Random;/**
* {@link FileControl_access} test case.
*
* @author Someone
* @version Somerevision
*/
public class TestFileControl_access extends TestCase {
private static final String TEST_DATA = "/testdata/FileControl_access.test.data";
private static final String testDataFile;
private static final String destPath; /**
* Initialize the test data file name at the static initialization block.
* It will search the classpath for test data file specified by {@link #TEST_DATA}.
* @throws RuntimeException If test data file cannot be found.
*/
static {
URL url = TestFileControl_access.class.getResource(TEST_DATA);
if (null == url) {
throw new RuntimeException("Test data file not ready.");
}
testDataFile = url.getFile(); String path = TestFileControl_access.class.getProtectionDomain().getCodeSource().getLocation().getFile();
String absolutePath = new File(path).getAbsolutePath().replace('\\', '/');
destPath = absolutePath.endsWith("/") ? absolutePath : absolutePath + "/";
} /**
* Random object, for random file name generation.
*/
private final Random random = new Random(System.currentTimeMillis());
public TestFileControl_access(String name) {super(name);} /**
* Test copyFile method.
* @see FileControl_access#copyFile(File, File)
* @see FileControl_access#copyFile(String, String)
*/
public void testCopyFile() {
// 1. assert copy correctly
File srcFile = new File(testDataFile);
byte[] srcBytes = readTheSpecifiedFile(srcFile);
File dstFile = new File(generateRandomFileName());
assertFalse(dstFile.exists());
assertTrue(new FileControl_access().copyFile(srcFile, dstFile));
assertTrue(dstFile.exists());
byte[] dstBytes = readTheSpecifiedFile(dstFile);
assertTrue(Arrays.equals(srcBytes, dstBytes));
removeSpecifiedFile(dstFile); // 2. go on others ...
} // private : /**
* Read given file into byte array. And ignore the argument checking.
* If some IO error occurs, just throw out the RuntimeException directly.
* @param source Source file.
* @return byte array of given file.
*/
private byte[] readTheSpecifiedFile(File source) {
try {
FileInputStream fis = new FileInputStream(source);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while ((ch = fis.read()) != -1) {
bos.write(ch);
}
byte[] result = bos.toByteArray();
fis.close();
bos.close();
return result;
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
} /**
* Generate random file name (Uppercase characters).
* @return Random file name, never return null.
*/
private String generateRandomFileName() {
final int fileNameLength = 8;
StringBuffer result = new StringBuffer();
for (int i = 0; i < fileNameLength; i++) {
char each = (char) ('A' + random.nextInt(26));
result.append(each);
}
return destPath + result.toString();
} /**
* Delete the given file.
* @param file The file will be erased.
*/
private void removeSpecifiedFile(File file) {
file.delete();
}
}
>>
来信中你提到你刚开始学习测试,其实这些内容都应该是开发人员来做的.:)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货