为了账号安全,请及时绑定邮箱和手机立即绑定

有没有办法模拟 getCTTbl().getTblPr().getTblBorders().

有没有办法模拟 getCTTbl().getTblPr().getTblBorders().

慕神8447489 2023-06-14 16:16:19
我写了一个将数据写入word文档的类。现在我必须为我的班级编写 mockito 测试。我的问题是我不知道如何在模拟表上模拟 getCTTbl().getTblPr().getTblBorders().getBottom().setColor()。这是我尝试为其编写测试的方法的一部分。public  void populateDocumentWithProfileSkills(XWPFDocument document, ExportProfileDTO profileData){        XWPFTable antet = document.createTable();        antet.getCTTbl().getTblPr().getTblBorders().getBottom().setColor(COLOR_OF_TABLE_BORDERS);        antet.getCTTbl().getTblPr().getTblBorders().getRight().setColor(COLOR_OF_TABLE_BORDERS);        antet.getCTTbl().getTblPr().getTblBorders().getLeft().setColor(COLOR_OF_TABLE_ANTET_BACKGROUND);        antet.getCTTbl().getTblPr().getTblBorders().getTop().setColor(COLOR_OF_TABLE_ANTET_BACKGROUND);...}到目前为止我所做的是:@Before    public void setup() {        MockitoAnnotations.initMocks(this);        exportProfileDTO = makeExportProfileDto();        mockDocument = mock(XWPFDocument.class);        mockTable = mock(XWPFTable.class);    }    @Test    public void populateDocumentWithProfileSkills(){        when(mockDocument.createTable()).thenReturn(mockTable);proffesionalSumaryService.populateDocumentWithProfileSkills(mockDocument,exportProfileDTO);    }如果我说CTTbl mockCTTbl = mock(CTTbl.class);when(mockTable.getCTTbl()).thenReturn(mockCTTbl);我将不胜感激有关如何执行此操作的任何建议,或者可能是有关如何测试此类的更好方法。
查看完整描述

2 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

http://poi.apache.org/components/index.html

poi-ooxml 需要 poi-ooxml-schemas。这是 ooxml-schemas jar 的一个小得多的版本(ooxml-schemas-1.4.jar 用于 POI 4.0.0 或更高版本,ooxml-schemas-1.3.jar 用于 POI 3.14 或 POI 3.17,ooxml-schemas-1.1.jar POI 3.7 至 POI 3.13,ooxml-schemas-1.0.jar 用于 POI 3.5 和 3.6)。较大的 ooxml-schemas jar 通常只需要用于开发。同样,ooxml-security jar 包含所有与加密和签名相关的类,通常只在开发时需要。其内容的一个子集在 poi-ooxml-schemas 中。这个 JAR 是 ooxml-security-1.1.jar 用于 POI 3.14 及之前的 ooxml-security-1.0.jar。

这基本上是说您需要将匹配的ooxml-schemasjar 添加到您的 pom 才能访问所有相关类。

你可能想使用不同的范围,因为它说它只是开发所必需的,但你必须自己验证。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.3</version>
</dependency>


查看完整回答
反对 回复 2023-06-14
?
富国沪深

TA贡献1790条经验 获得超9个赞

我假设您必须在设置方法中以下面的方式使用 Mockito.RETURNS_DEEP_STUBS 选项

 mockDocument = mock(XWPFDocument.class);
 mockTable = mock(XWPFTable.class, Mockito.RETURNS_DEEP_STUBS);

因此 Mockito 框架会为每个 get 调用返回一个模拟,get 调用不必是静态的。


查看完整回答
反对 回复 2023-06-14
  • 2 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信