我有以下代码:
@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
方法。你知道问题到底是什么吗?怎么解决?
1条答案
按热度按时间drnojrws1#
我只是执行你代码,它工作正常
检查随附的屏幕截图
的数据
的