gpt4 book ai didi

java - 尽管文件存在,Mockito 返回 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-01 21:16:29 24 4
gpt4 key购买 nike

我正在尝试对新创建的 Spring Boot 服务类运行测试。 StringWriter 内容是常规 XML,带有几行名为“transaction”的标签。

PrepareExcelServiceTest.class

@SpringBootTest
@RunWith(MockitoJUnitRunner.class)
public class PrepareExcelServiceTest {

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

private static final Logger log = LoggerFactory.getLogger(PrepareExcelService.class);

@Test
public void testPrepareExcelService() throws ParserConfigurationException, SAXException, IOException {
PrepareExcelService prepareExcelService = new PrepareExcelService();
StringWriter xmlStringWriter = new StringWriter();

Source source = new StreamSource("./view/xml/0_dummy.xml");
xmlStringWriter.write(source.toString());

prepareExcelService.prepareDocument(xmlStringWriter);
}
}

PrepareExcelService.class

@Service
public class PrepareExcelService {

private HSSFWorkbook workbook;
private HSSFSheet spreadsheet;

public void prepareDocument(StringWriter xmlStringWriter) throws IOException,
SAXException, ParserConfigurationException {
workbook = setupWorkBook();
spreadsheet = setupSheet(workbook);

Document document = parseXMLStringWriter(xmlStringWriter);
fillContent(document, spreadsheet);

FileOutputStream output = new FileOutputStream(new File("TestExcelFile.xls"));
workbook.write(output);
output.flush();
output.close();
}

private HSSFWorkbook setupWorkBook() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle cellStyle = workbook.createCellStyle();

cellStyle.setBorderLeft(BorderStyle.MEDIUM);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
cellStyle.setBorderTop(BorderStyle.MEDIUM);
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setIndention((short)4);
cellStyle.setWrapText(true);

HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(false);
cellStyle.setFont(font);

return workbook;
}

private HSSFSheet setupSheet(HSSFWorkbook workbook) {
HSSFSheet spreadSheet = workbook.createSheet("spreadSheet");
return spreadSheet;
}

private Document parseXMLStringWriter(StringWriter xmlStringWriter) throws ParserConfigurationException,
IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

return builder.parse(xmlStringWriter.toString());
}

private void fillContent (Document document, HSSFSheet spreadSheet) {
HSSFRow row = spreadSheet.createRow(0);
HSSFCell cell = row.createCell((short) 1);

NodeList nodeList = document.getElementsByTagName("transaction");
HSSFRow rowOne = spreadSheet.createRow(1);

cell.setCellValue("Spreadsheet Header Row");

for (int i = 0; i < nodeList.getLength(); i++) {
switch (i) {
case 0:
cell = rowOne.createCell((short) 0);
cell.setCellValue("transaction");
cell = rowOne.createCell((short) 1);
cell.setCellValue(((Element) (nodeList.item(0))).getElementsByTagName("transaction").item(0)
.getFirstChild().getNodeValue());
break;
default:
break;
}
}
}
}

在测试此服务时,尽管文件存在并且其他服务通过 StringWriter 与它正确交互,但我收到以下异常。

java.io.FileNotFoundException: ...\javax.xml.transform.stream.StreamSource@4439f31e (The system cannot find the file specified)

at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:623)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:812)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at ee.estcard.repgen.service.PrepareExcelService.parseXMLStringWriter(PrepareExcelService.java:78)
at ee.estcard.repgen.service.PrepareExcelService.prepareDocument(PrepareExcelService.java:39)
at ee.estcard.repgen.service.PrepareExcelServiceTest.testPrepareExcelService(PrepareExcelServiceTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:16)
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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我认为问题在于我对 StringWriter 的错误使用,但由于我对 Java 的菜鸟知识,我仍然无法解决这个问题。

最佳答案

不,您的问题与您如何使用StreamSource有关,确实调用source.toString()不会为您提供您所期望的源内容,而是类似 javax.xml.transform.stream.StreamSource@4439f31e 的内容正如您在堆栈跟踪中看到的那样。

只需删除您的 StringWriter并使用 InputSource在您的代码中修复您的代码,同时也使其更加灵活,因为使用 InputSource是对许多不同类型的源开放的大门,此外,加载文件的内容不是一个好的做法,应该避免,因为如果文件太大,如果无法放入堆中,您可能会面临 OOME。

PrepareExcelServiceTest

PrepareExcelService prepareExcelService = new PrepareExcelService();
InputSource source = new InputSource("./view/xml/0_dummy.xml");
prepareExcelService.prepareDocument(source);

在方法parseXMLStringWriter中更名

private Document parseXML(InputSource source) throws ParserConfigurationException,
IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

return builder.parse(source);
}

在方法prepareDocument

public void prepareDocument(InputSource source) throws IOException,
SAXException, ParserConfigurationException {
...

Document document = parseXML(source);

...
}

关于java - 尽管文件存在,Mockito 返回 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39955245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com