Junit 在PowerMock中模拟类

Junit 在PowerMock中模拟类,junit,powermock,Junit,Powermock,我正在为JUNIT使用PowerMocking,我是PowerMock的新手 我想模拟一个非静态的类 类场景如下所示 public class Export extends MyUtil implements ExportFormatting<DeptSummaryByDCDTO, LmReportsInputDTO>{ public String createPDF(List<DeptSummaryByDCDTO> summaryDtoList, LmRep

我正在为JUNIT使用PowerMocking,我是PowerMock的新手

我想模拟一个非静态的类

类场景如下所示

 public class Export extends MyUtil implements ExportFormatting<DeptSummaryByDCDTO, LmReportsInputDTO>{

    public String createPDF(List<DeptSummaryByDCDTO> summaryDtoList, LmReportsInputDTO inputDto){

     }

    public String createPDF(Map<String, DeptSummaryByDCDTO> paramMap,
        LmReportsInputDTO paramK) {


    }
 public static Response getMultiplePackSku{
       filePath = new Export(inputDto).createPDF(resultList,null);
 }
问题是,

我正在尝试使用powermock测试上述类


任何人都可以告诉我们如何模拟行文件路径…..

下面是如何模拟构造函数调用的说明:

下面是如何模拟构造函数调用的说明:

您需要先模拟构造函数并返回
导出
模拟。在返回的模拟上,您需要记录对
createPDF
的调用。棘手的部分是构造函数模拟。我给你举个例子,希望你能得到所有这些:

@RunWith(PowerMockRunner.class) // This annotation is for using PowerMock
@PrepareForTest(Export.class) // This annotation is for mocking the Export constructor
public class MyTests {

    private mockExport;

    @Before
    public void setUp () {
        // Create the mock
        mockExport = PowerMock.createMock(Export.class)
    }

    @Test
    public void testWithConstructor() {
        SomeDtoClass inputDto = PowerMock.createMock(SomeDtoClass.class); 
        PowerMock.expectNew(Export.class, inputDto).andReturn(mockExport);
        PowerMock.replay(mockExport, Export.class);
        expect(mockExport.createPDF(resultList, null);


        // Run the tested method.
    }

}

您需要首先模拟构造函数并返回一个
Export
mock。在返回的模拟上,您需要记录对
createPDF
的调用。棘手的部分是构造函数模拟。我给你举个例子,希望你能得到所有这些:

@RunWith(PowerMockRunner.class) // This annotation is for using PowerMock
@PrepareForTest(Export.class) // This annotation is for mocking the Export constructor
public class MyTests {

    private mockExport;

    @Before
    public void setUp () {
        // Create the mock
        mockExport = PowerMock.createMock(Export.class)
    }

    @Test
    public void testWithConstructor() {
        SomeDtoClass inputDto = PowerMock.createMock(SomeDtoClass.class); 
        PowerMock.expectNew(Export.class, inputDto).andReturn(mockExport);
        PowerMock.replay(mockExport, Export.class);
        expect(mockExport.createPDF(resultList, null);


        // Run the tested method.
    }

}

downvoter,下次请提供关于whydownvoter的评论,下次请提供关于为什么的评论