Java 尝试使用junit5和mockito模拟中的字节[]值时为空值
我试图模拟一个Java 尝试使用junit5和mockito模拟中的字节[]值时为空值,java,null,mockito,junit5,Java,Null,Mockito,Junit5,我试图模拟一个字节,但在该方法中,该值为null。我正在使用eq()方法发送我需要测试此方法的字节[] 这是我的方法的签名: @Override public void addFile(String directory, String fileName, byte[] content) throws IOException { try (SftpSession session = sessionFactory.getSession()) {
字节
,但在该方法中,该值为null
。我正在使用eq()
方法发送我需要测试此方法的字节[]
这是我的方法的签名:
@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(字节)
传递给正在测试的方法
相反,我建议使用ArgumentCaptor
,并查看正在使用正确参数调用的会话。write()
(您实际上正在将其传递到sftpservicecimpl.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());
}
@Mock
SftpSession SftpSession;
@嘲弄
DefaultSftpSessionFactory会话工厂;
@俘虏
逮捕人;
专用SFTPServiceImpl SFTPServiceImpl;
@之前
公共void init(){
initMocks(this);
sftpServiceImpl=新的sftpServiceImpl(sessionFactory);
}
@试验
void addFile()引发异常{
byte[]bytes={69,121,101,45,62,118,101,114,(byte)196,(byte)195,61,101,98};
给定(sessionFactory.getSession())。将返回(sftpSession);
给定(sftpSession.exists(anyString())。将返回(false);
String directory=“testDirectory”;
字符串fileName=“testFileName”;
sftpServiceImpl.addFile(目录、文件名、字节);
验证(sftpSession,times(1)).write(captor.capture(),eq(目录+“/”+文件名));
//这里需要比较字节数组
AssertArrayQuals(字节,captor.getValue());
}
希望这是有意义的。如果您试图测试
sftpservicecimpl.addFile()
,那么您是做错了。您不应将anyString()
和eq(字节)
传递给正在测试的方法
相反,我建议使用ArgumentCaptor
,并查看正在使用正确参数调用的会话。write()
(您实际上正在将其传递到sftpservicecimpl.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());
}
@Mock
SftpSession SftpSession;
@嘲弄
DefaultSftpSessionFactory会话工厂;
@俘虏
逮捕人;
专用SFTPServiceImpl SFTPServiceImpl;
@之前
公共void init(){
initMocks(this);
sftpServiceImpl=新的sftpServiceImpl(sessionFactory);
}
@试验
void addFile()引发异常{
byte[]bytes={69,121,101,45,62,118,101,114,(byte)196,(byte)195,61,101,98};
给定(sessionFactory.getSession())。将返回(sftpSession);
给定(sftpSession.exists(anyString())。将返回(false);
String directory=“testDirectory”;
字符串fileName=“testFileName”;
sftpServiceImpl.addFile(目录、文件名、字节);
验证(sftpSession,times(1)).write(captor.capture(),eq(目录+“/”+文件名));
//这里需要比较字节数组
AssertArrayQuals(字节,captor.getValue());
}
希望这是有意义的。您是否正在尝试测试
sftpServiceImpl.addFile()
?是的,我正在尝试测试sftpServiceImpl.addFile(),而不是回复注释,我想我将展开并编写一个答案。希望对您有所帮助。您是否正在尝试测试sftpServiceImpl.addFile()
?是的,我正在尝试测试sftpServiceImpl.addFile(),而不是回复注释,我想我将展开并编写一个答案。希望它能帮助您。是的,它帮助很大,但我怀疑会话变量是从哪里获得的,而且assertArrayEquals似乎不接受此参数。谢谢我更新了答案,在测试中它被称为sftpSession
。是的,您可以使用想要比较数组的任何方法:)。我只是在stackoverflow答案部分写了这个答案,没有使用任何实际的IDE:)是的,它帮助很大,但我怀疑的是会话变量是从哪里得到的,assertArrayEquals似乎不接受这个参数。谢谢我更新了答案,在测试中它被称为sftpSession
。是的,您可以使用想要比较数组的任何方法:)。我只是在stackoverflow答案部分写了这个答案,没有使用任何实际的IDE:)