Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用junit5和mockito模拟中的字节[]值时为空值_Java_Null_Mockito_Junit5 - Fatal编程技术网

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:)