如何使powermock与Java11一起使用?

时间:2019-04-17 09:18:13

标签: powermock java-11

我有一个用Java11编写的服务项目。它正在使用junit测试来覆盖全部代码覆盖范围。 我已经在updateFiles类中将调度程序方法写入FileReceivingStateMarkerService(由Files.walkFileTree调用)。我想从为Files.walkFileTree编写的测试类中测试静态调用FileReceivingStateMarkerService。我需要使用PowerMockRunner来测试静态调用,因为它无法通过简单的MockitoJunitRunner进行测试。

在运行测试时,我在第mockStatic(Files.class);行的setUp方法中获得了NPE。

您能帮我解决有关如何使用Java11配置powermock并初始化静态类的问题吗?

我在服务项目(在Java11中)中配置了powermock,如下所示

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.0-RC.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.0-RC.1</version>
    <scope>test</scope>
</dependency>

FileReceivingStateMarkerService class

class FileReceivingStateMarkerService {
    private static final Logger LOG = LoggerFactory.getLogger(FileReceivingStateMarkerService.class);

    private final FileProcessingActivityRepositoryService fileProcessingActivityRepositoryService;
    private final FilePollActivityRepository filePollActivityRepository;
    private final FileProcessingRequestRepositoryService fileProcessingRequestRepositoryService;

    @Autowired
    FileReceivingStateMarkerService(FileProcessingActivityRepositoryService fileProcessingActivityRepositoryService, FilePollActivityRepository filePollActivityRepository,
                                           FileProcessingRequestRepositoryService fileProcessingRequestRepositoryService) {
        this.fileProcessingActivityRepositoryService = fileProcessingActivityRepositoryService;
        this.filePollActivityRepository = filePollActivityRepository;
        this.fileProcessingRequestRepositoryService = fileProcessingRequestRepositoryService;
    }

    @Scheduled(fixedDelayString="#{${fileprocess.filemonitor.interval.minutes}*60*1000}")
    void updateFiles() {
        List<FileProcessingRequest> requests = fileProcessingRequestRepositoryService.getFileProcessingRequests();
        LOG.debug("Running file receiving state");
        requests.forEach(request -> {
            LOG.debug("Looking at location {}", request.getFileLocation());
            final var fileVisitor = new FileReceivingStateMarkerFileVisitor(filePollActivityRepository
                                                                          , fileProcessingActivityRepositoryService
                                                                          , request);
            try {
                Files.walkFileTree(Path.of(request.getFileLocation()), Collections.emptySet(), 1, fileVisitor);
            } catch(Exception e) {
                LOG.debug(e.getMessage());
            }
        });
    }
}

FileReceivingStateMarkerService的测试类

package com.experianhealth.rfp.scheduler;

import com.experianhealth.rfp.core.DocumentType;
import com.experianhealth.rfp.core.FileProcessingRequest;
import com.experianhealth.rfp.dao.FilePollActivityRepository;
import com.experianhealth.rfp.service.FileProcessingActivityRepositoryService;
import com.experianhealth.rfp.service.FileProcessingRequestRepositoryService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
@PrepareForTest(FileReceivingStateMarkerService.class)
public class FileReceivingStateMarkerServicePowerTest {

    @Mock
    private FileProcessingActivityRepositoryService fileProcessingActivityRepositoryService;
    @Mock
    private FilePollActivityRepository filePollActivityRepository;
    @Mock
    private FileProcessingRequestRepositoryService fileProcessingRequestRepositoryService;

    @Before
    public void setUp() throws Exception {
        mockStatic(Files.class);
    }

    @Test
    public void updateFiles() throws Exception {
        String rootDir = "C:\\files_processor";
        List<FileProcessingRequest> requests = new ArrayList<>();
        FileProcessingRequest request = new FileProcessingRequest();
        request.setClientId("123");
        request.setTradingPartnerId("123");
        request.setDocumentId(DocumentType.Payer);
        request.setFileLocation(rootDir);
        requests.add(request);
        final var fileVisitor = new FileReceivingStateMarkerFileVisitor(filePollActivityRepository
                , fileProcessingActivityRepositoryService
                , request);
        try {
            PowerMockito.when(Files.walkFileTree(Path.of(request.getFileLocation() ), Collections.emptySet(), 1, fileVisitor))
                    .thenReturn(any());
        } catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

0 个答案:

没有答案