Sping Boot Mockito when()需要一个参数,该参数必须是'a method call on a mock'

hl0ma9xz  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(82)

我有以下代码:

@Configuration
@ConfigurationProperties(prefix = "prf")
public class AppPropertiesConfiguration {

  private String directoryPath;

    //Getter  Setter
}

@Service
public class FileService {

  private final AppPropertiesConfiguration appPropertiesConfiguration;

  public FileService(final AppPropertiesConfiguration appPropertiesConfiguration) {

    this.appPropertiesConfiguration = appPropertiesConfiguration;
  }

  public void writeFile(final byte[] byteFiletoSave, final String fileName) {

    final String directoryPath = appPropertiesConfiguration.getDirectoryPath();

    final Path path = Paths.get(directoryPath, fileName);

    try {
      Files.write(path, byteFiletoSave);
    } catch (IOException e) {
      logger.error("Error while saving file with name {} in NAS dute to : ", fileName, e);
    }
  }
}

字符串
这是我的联合测试

@ExtendWith(MockitoExtension.class)
public class FileServiceTest {

  @InjectMocks
  FileService fileService;

  @Mock
  AppPropertiesConfiguration appPropertiesConfigurationMock;

    @Test
    void should_write_file() {

      //Given

      final byte[] docByte = new byte[0];
      final String fileName = "justFilename.pdf";
      final String dirPath = "C:\\dir1\\dir2\\dir3";

      when(appPropertiesConfigurationMock.getDirectoryPath()).thenReturn(dirPath);

      //When
      fileService.writeFile(docByte, fileName);

      //Then

    }
}


我得到这个错误:when() requires an argument which has to be 'a method call on a mock'事件,我真的传递了一个模拟对象给when方法。你知道问题到底是什么吗?怎么解决?

drnojrws

drnojrws1#

我只是执行你代码,它工作正常
检查随附的屏幕截图


的数据


相关问题