我试图对覆盖文件的覆盖方法进行单元测试(如果给定文件不存在,则创建新方法(。我的想法是在覆盖之前和覆盖之后比较两个数组,所以我做到了,但不幸的是,它说实际数组是空的。我正在使用临时文件夹,以便在测试后可以轻松删除它。
方法我想单元测试+测试覆盖方法所需的方法。
package rankingsystem;
import contentfile.ContentFileRetriever;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class RankingSystemService {
private ContentFileRetriever contentFileRetriever;
public RankingSystemService(ContentFileRetriever contentFileRetriever) {
this.contentFileRetriever = contentFileRetriever;
}
...
String[] retrieveRankingData(String rankingPathFile) {
return contentFileRetriever.getContent(rankingPathFile);
}
void overwriteFileWithGivenResult(String name, long timeOfFinishingGame, String rankingPathFile) {
try (FileWriter writer = new FileWriter(rankingPathFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
bufferedWriter.write(name + " " + timeOfFinishingGame);
bufferedWriter.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试:
public class RankingSystemServiceTest {
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Mock
ContentFileRetriever contentFileRetriever;
private RankingSystemService rankingSystemService = new RankingSystemService(contentFileRetriever);
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
rankingSystemService = new RankingSystemService(contentFileRetriever);
}
@Test
public void overwriteFileWithGivenResult() throws IOException {
File rankingFile = tempFolder.newFile("rankingText.txt");
String rankingFilePath = rankingFile.getPath();
String[] actualResultBeforeOverwriting = rankingSystemService.retrieveRankingData(rankingFilePath);
String[] expectedResultBeforeOverwriting = {};
assertArrayEquals(expectedResultBeforeOverwriting, actualResultBeforeOverwriting);
rankingSystemService.overwriteFileWithGivenResult("Piotr", 1L, rankingFilePath);
String[] afterOverwriting = rankingSystemService.retrieveRankingData(rankingFilePath);
String[] expectedResultAfterOverwriting = {"Piotr 1"};
assertArrayEquals(expectedResultAfterOverwriting, afterOverwriting);
}
检索内容getContent
:
@Override
public String[] getContent(String pathName) {
try (Stream<String> contentFileStream = Files.lines(Paths.get(pathName))) {
return contentFileStream.toArray(String[]::new);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
堆栈跟踪:
java.lang.AssertionError: actual array was null
at org.junit.Assert.fail(Assert.java:88)
at org.junit.internal.ComparisonCriteria.assertArraysAreSameLength(ComparisonCriteria.java:71)
at org.junit.internal.ComparisonCriteria.arrayEquals(ComparisonCriteria.java:37)
at org.junit.Assert.internalArrayEquals(Assert.java:532)
at org.junit.Assert.assertArrayEquals(Assert.java:283)
at org.junit.Assert.assertArrayEquals(Assert.java:298)
at rankingsystem.RankingSystemServiceTest.overwriteFileWithGivenResult(RankingSystemServiceTest.java:61)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
根据JB Nizet用户的说法,我需要删除ContentFileRetriever的模拟并创建ContentFileRetriever的实例,它可以工作。
ContentFileRetrieverService contentFileRetrieverService = new ContentFileRetrieverService();