我有过
@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");
}
}我的FileInputStreamFactory:
@Component
public class FileInputStreamFactory {
public FileInputStream getStream(final String file) throws FileNotFoundException {
return new FileInputStream(file);
}
}我的测验
@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();
}为什么我的f总是空的?我试过很多种变体。但它总是空的。我为FileInputStream创建了工厂,因为我不想在测试中使用PowerMock在新FileInputStream上模拟创建。
发布于 2019-08-12 07:51:41
我明白了我同时使用@RunWith(SpringRunner.class)和MockitoAnnotations.initMocks(this);的错误是什么。如果两者都使用,那么mokcks已经创造了,但工作方式不正确。所以我刚删除了MockitoAnnotations.initMocks(this);
发布于 2019-08-09 14:44:28
我在下面的例子中复制了你的测试,这个例子对我有用。
(用JUnit 4 & Mockito 3.0.0和2.28.2验证)
我唯一改变的是Runner,但根据您的评论,您已经这样做了。
我删除了示例中的注释,因为它们在使用MockitoJUnitRunner执行测试时不相关,并更改了loadMappingsFromSource的返回类型以轻松添加Assert.assertNotNull。我还用一个CodesConverterException替换了RuntimeException。
这些更改都不应影响测试本身。
mock for FileInputStream是正确创建的,即使只有带有参数的构造函数。
@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());
}
}发布于 2019-08-09 14:07:36
1)试试下面的代码:
@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();
}https://stackoverflow.com/questions/57430893
复制相似问题