尝试使用 junit5 和 mockito 模拟 byte[] 值时的空值

Null value when trying to mock a byte[] value in with junit5 and mockito

我正在尝试模拟一个 byte,但在方法中这个值是 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());
}

希望这是有道理的。