我正在尝试模拟一个字节
,但在该方法中该值为null
。我正在使用 eq()
方法发送测试此方法所需的 byte[]
。
这是我的方法的签名:
@Override
public void addFile(String directory, String fileName, byte[] content) throws IOException {
try (SftpSession session = sessionFactory.getSession()) {
makeSureDirectoryIsAvailable(session, directory);
InputStream fileByteStream = new ByteArrayInputStream(content);
session.write(fileByteStream, directory + "/" + fileName);
}
}
当我查看内容变量时,它是 null
并且字符串为空,我不太确定如何模拟这个值,接下来是我的测试代码,我正在使用 JUnit 5。
package service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.integration.sftp.session.SftpSession;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class SFTPServiceImplTest {
@Mock
SftpSession sftpSession;
@Mock
DefaultSftpSessionFactory sessionFactory;
private SFTPServiceImpl sftpServiceImpl;
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
sftpServiceImpl = new SFTPServiceImpl(sessionFactory);
}
@Test
void addFile() throws Exception {
byte[] bytes = {69, 121, 101, 45, 62, 118, 101, 114, (byte) 196, (byte) 195, 61, 101, 98};
given(sessionFactory.getSession()).willReturn(sftpSession);
given(sftpSession.exists(anyString())).willReturn(false);
sftpServiceImpl.addFile(anyString(), anyString(),eq(bytes));
}
@Test
void getFileContent() {
}
@Test
void deleteFile() {
}
@Test
void moveFile() {
}
@Test
void listFiles() {
}
}
最佳答案
如果您尝试测试sftpServiceImpl.addFile()
,那么您就做错了。您不应将 anyString()
和 eq(bytes)
传递给您正在测试的方法。
相反,我建议使用ArgumentCaptor
并查看使用正确的参数调用session.write()
(您实际上将其传递给sftpServiceImpl .addFile()
)
代码看起来像这样 -
@Mock
SftpSession sftpSession;
@Mock
DefaultSftpSessionFactory sessionFactory;
@Captor
ArgumentCaptor<InputStream> captor;
private SFTPServiceImpl sftpServiceImpl;
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
sftpServiceImpl = new SFTPServiceImpl(sessionFactory);
}
@Test
void addFile() throws Exception {
byte[] bytes = {69, 121, 101, 45, 62, 118, 101, 114, (byte) 196, (byte) 195, 61, 101, 98};
given(sessionFactory.getSession()).willReturn(sftpSession);
given(sftpSession.exists(anyString())).willReturn(false);
String directory = "testDirectory";
String fileName = "testFileName";
sftpServiceImpl.addFile(directory, fileName, bytes);
verify(sftpSession, times(1)).write(captor.capture(), eq(directory + "/" + fileName));
// you need to compare the byte arrays here
assertArrayEquals(bytes, captor.getValue());
}
希望这是有道理的。
关于java - 尝试使用 junit5 和 mockito 模拟 byte[] 值时出现空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59759333/