java - 尝试使用 junit5 和 mockito 模拟 byte[] 值时出现空值

标签 java null mockito junit5

我正在尝试模拟一个字节,但在该方法中该值为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/

相关文章:

java - 如何在没有数据库的情况下创建 jasper 报告

c# - 如何检查数据行值是否为空

typescript 不会因错误的 promise 返回类型而抛出错误

android - "Mockito 0 matchers expected, 3 recorded"- 为什么需要 0 个匹配器?

android - Mockito:在 UI 测试中模拟最终类

java - 将 Map<String, String> 转换为 List<NameValuePair> - 这是最有效的吗?

使用从 URLConnection 读取的值的 Java If 语句不起作用

java - Apache POI 解析和处理空单元格

haskell - 我可以在 Haskell 中声明一个 NULL 值吗?

spring-data-jpa - java.lang.AssertionError : No value at JSON path "$[0].sAcctDesc" 错误