Java 无法模拟FileInputStream
我有 我的FileInputStreamFactory:Java 无法模拟FileInputStream,java,spring,junit,mockito,Java,Spring,Junit,Mockito,我有 我的FileInputStreamFactory: @Component public class CodesConverterService { private final FileInputStreamFactory fileInputStreamFactory; public CodesConverterService(FileInputStreamFactory fileInputStreamFactory, YamlFactory yamlFactory) {
@Component
public class CodesConverterService {
private final FileInputStreamFactory fileInputStreamFactory;
public CodesConverterService(FileInputStreamFactory fileInputStreamFactory, YamlFactory yamlFactory) {
this.fileInputStreamFactory = fileInputStreamFactory;
}
@EventListener(ApplicationReadyEvent.class)
public void loadMappingsFromSource() {
try {
FileInputStream f = fileInputStreamFactory.getStream("mappingFile");
} catch (Exception e) {
throw new CodesConverterException("Can`t load mappings from source");
}
}
我的测试
@Component
public class FileInputStreamFactory {
public FileInputStream getStream(final String file) throws FileNotFoundException {
return new FileInputStream(file);
}
}
为什么我的f总是空的?我试过分配各种变体。但它总是空的。我已经为FileInputStream创建了工厂,因为我不想在测试中使用PowerMock来模拟新FileInputStream的创建。FileInputStream没有无参数构造函数。这就是Mockito无法实例化mock的原因 您可以尝试使其成为间谍,并相应地模拟所需的方法:
@RunWith(SpringRunner.class)
public class CodesConverterServiceTest {
@InjectMocks
private CodesConverterService codesConverterService;
@Mock
private FileInputStreamFactory fileInputStreamFactory;
@Mock
private FileInputStream fileInputStream;
@Test
public void whenLoadMappingsFromSource_GoodPath() throws FileNotFoundException {
when(fileInputStreamFactory.getStream("mappingFile")).thenReturn(fileInputStream);
this.codesConverterService.loadMappingsFromSource();
}
编辑:
或者,您可以通过以下方式在测试方法中创建模拟:
@Spy
private FileInputStream fileInputStreamSpy = new FileInputStream("dummy");
1) 请尝试以下代码:
FileInputStream fileInputStreamMock =
Mockito.mock(FileInputStream.class, withSetting().useConstructor("dummy"));
我在下面的示例中复制了您的测试,这对我很有用。
(通过JUnit4和Mockito验证) 我唯一改变的是
跑步者
,但根据您的评论,您已经这样做了
我删除了示例中的注释,因为当使用MockitoJUnitRunner
执行测试时,它们应该是无关的,并且更改了loadMappingsFromSource
的返回类型,以便轻松添加Assert.assertNotNull
。
我还将CodesConverterException
替换为RuntimeException
这些更改都不会影响测试本身
FileInputStream
的mock
被正确创建,即使只有带参数的构造函数
@Test
public void itShouldReturnFileInputStream() {
FileInputStreamFactory mockFileInputStreamFactory = mock(FileInputStreamFactory.class);
FileInputStream mockFileInputStream = mock(FileInputStream.class);
Mockito.doReturn(mockFileInputStream).when(mockFileInputStreamFactory).getStream("fileName");
CodesConverterService codeConverterService = new CodesConverterService(mockFileInputStreamFactory);
assertThatCode(() -> codeConverterService.codeConverterService()).doesNotThrowAnyException();
}
我弄清楚了我在使用
@RunWith(SpringRunner.class)
和MockitoAnnotations.initMocks(this)时犯了什么错误代码>同时进行。如果两者都使用,那么mokcks已经创建了,但工作方式不正确。所以我刚刚删除了MockitoAnnotations.initMocks(this)代码>尝试when(fileInputStreamFactory.getStream(“mappingFile”))。然后返回(fileInputStream)对不起,这是我写问题时的错误。我编辑了我的问题但是你确定你的模拟被注射了吗?您正在使用SpringRunner进行Mockito测试。将@RunWith(SpringRunner.class)
更改为@RunWith(MockitoJUnitRunner.class)
。仍然为空我认为您需要添加一个带有MockitoAnnotations.initMocks(this)
的安装方法,以便您的mock真正被模拟。但是我不能写它,因为它说未处理的异常文件notfoundtry the alternative我看到创建了工厂模拟,但它总是f=null。即使有其他选择。谢谢你的建议。试着在测试类中使用Mockito.spySame problem和yamlFactory.getYaml()创建一个实际的间谍。但是public Yaml getYaml(){return new Yaml()没有参数构造函数FileInputStreamFactory
默认有一个默认构造函数,因为他没有声明任何其他构造函数。。。
@RunWith(MockitoJUnitRunner.class)
public class CodesConverterServiceTest {
class YamlFactory {
}
class FileInputStreamFactory {
public FileInputStream getStream(final String file) throws FileNotFoundException {
return new FileInputStream(file);
}
}
static class CodesConverterService {
private final FileInputStreamFactory fileInputStreamFactory;
public CodesConverterService(FileInputStreamFactory fileInputStreamFactory, YamlFactory yamlFactory) {
this.fileInputStreamFactory = fileInputStreamFactory;
}
public FileInputStream loadMappingsFromSource() {
try {
return fileInputStreamFactory.getStream("mappingFile");
} catch (Exception e) {
throw new RuntimeException("Can`t load mappings from source");
}
}
}
@InjectMocks
private CodesConverterService codesConverterService;
@Mock
private FileInputStreamFactory fileInputStreamFactory;
@Mock
private FileInputStream fileInputStream;
@Test
public void whenLoadMappingsFromSource_GoodPath() throws FileNotFoundException {
Mockito.when(fileInputStreamFactory.getStream("mappingFile")).thenReturn(fileInputStream);
Assert.assertNotNull(codesConverterService.loadMappingsFromSource());
}
}