【发布时间】:2015-11-16 20:04:00
【问题描述】:
我有一个创建 ArrayList 的类 CollectionObject。
public class CollectionObject {
private List<String> collectionObject;
public CollectionObject() {
collectionObject = new ArrayList<String>();
}
public List<String> getCollectionObject() {
return collectionObject;
}
public void add(final String stringToWrite) throws VerifyException {
collectionObject.add(stringToWrite);
}
}
还有一个类接受 CollectionObject 类并使用它将文件的内容写入 CollectionObject 类。
public class ReaderFileWriterObjectService {
private BufferedReader bufferedReader;
private CollectionObject collectionObject;
private String line;
public CollectionObject getCollectionObjectAfterWritingFromAFile(final File file)
throws VerifyException, IOException {
collectionObject = new CollectionObject();
bufferedReader = new BufferedReader(new FileReader(file));
while ((line = bufferedReader.readLine()) != null) {
collectionObject.add(line);
}
bufferedReader.close();
return collectionObject;
}
如何测试和模拟ReaderFileWriterObjectService类的方法?
【问题讨论】:
-
你不能。
File与 JDK 绑定太多,因此测试它非常困难。但是,如果您使用 JSR 203,情况会大不相同...
标签: java unit-testing junit mockito