StringBuilder 损坏(内部字段 `count` = 0)

StringBuilder corrupted (internal field `count` = 0)

我为通过某些 writer 打印一些输出的方法编写了测试。 Writer 只是接口,实现 ConsoleWriterImpl 只是 System.out.

的包装器

测试目标: 检查是否所有应该打印的信息都已传递给 Writer.printLine(Object str)

问题

我使用 ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class); 来捕获对 Writer.printLine(Object str) 的输入。然后获取所有输入:List outputList = argument.getAllValues();.

该列表包含 2 种类型的对象:String 和 StringBuilders。然后我想将所有这些对象转换为一个字符串以用于测试目的。但是 outputList 中的所有 StringBuilder 都已损坏 — 它们的 count = 0。因此,当我尝试转换这些 StringBuilder 时,我得到的是空字符串。 查看下面的测试代码——我在问题所在处留下了评论。

问题:


ConsoleWriterImpl

public class ConsoleWriterImpl implements Writer {
    private PrintStream stream = System.out;

    public PrintStream getStream() {
        return stream;
    }

    public void setStream(PrintStream stream) {
        this.stream = stream;
    }

    @Override
    public void printLine(Object str) {
        stream.println(str);
    }
}

Test

import com.dtos.AccountDTO;
import com.dtos.ClientDTO;
import com.services.ClientService;
import com.view.io.Reader;
import com.view.io.Writer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ClientViewImplTest {
    private Writer writer;
    private Reader reader;
    private ClientService clientService;
    private ClientViewImpl clientView;

    @BeforeEach
    void setUp() {
        writer = mock(Writer.class);
        reader = mock(Reader.class);
        clientService = mock(ClientService.class);
        clientView = new ClientViewImpl(writer, reader, clientService);
    }

    @SuppressWarnings("unchecked")
    @Test
    void displayAllClientsInfo() throws ParseException {
        // Given
        DateFormat df = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy");
        List<ClientDTO> clients = new ArrayList<>();
        clients.add(new ClientDTO(1L, "John Smith", "client@example.com", Arrays.asList(
                new AccountDTO(10L, "JSmith1", "zzwvp0d9", df.parse("10:15:30 20.10.2017")),
                new AccountDTO(20L, "JSmith2", "mhjnbgfv", df.parse("10:15:30 5.5.2017")),
                new AccountDTO(30L, "JSmith3", "ytersds1", df.parse("15:00:30 12.10.2017"))
        )));
        clients.add(new ClientDTO(2L, "Jack Black", "jack@example.com", new ArrayList<>()));
        when(clientService.getAllClients()).thenReturn(clients);
        ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
        // When
        clientView.displayAllClientsInfo();
        // Then
        verify(writer, atLeast(1)).printLine(argument.capture());
        List outputList = argument.getAllValues();

        StringBuilder str = new StringBuilder(2000);
        for (Object sb : outputList) {
            str.append(sb); // here we got empty strings in case sb type's is StringBuilder
        }

        String output = str.toString();
        assertAll(
                // Client
                () -> assertTrue(output.contains(Long.toString(1))),
                () -> assertTrue(output.contains("client@example.com")),
                () -> assertTrue(output.contains("John Smith")),
                // Accounts
                () -> assertTrue(output.contains(Long.toString(10))),
                () -> assertTrue(output.contains("JSmith1")),
                () -> assertTrue(output.contains("zzwvp0d9")),
                () -> assertTrue(output.contains(df.parse("10:15:30 20.10.2017").toString())),
                () -> assertTrue(output.contains(Long.toString(20))),
                () -> assertTrue(output.contains("JSmith2")),
                () -> assertTrue(output.contains("mhjnbgfv")),
                () -> assertTrue(output.contains(df.parse("10:15:30 5.5.2017").toString())),
                () -> assertTrue(output.contains(Long.toString(30))),
                () -> assertTrue(output.contains("JSmith3")),
                () -> assertTrue(output.contains("ytersds1")),
                () -> assertTrue(output.contains(df.parse("15:00:30 12.10.2017").toString())),
                // Client
                () -> assertTrue(output.contains(Long.toString(2))),
                () -> assertTrue(output.contains("jack@example.com")),
                () -> assertTrue(output.contains("Jack Black"))
        );
    }
}

待测方法clientView.displayAllClientsInfo()

public void displayAllClientsInfo() {
    final Collection<ClientDTO> clients = clientService.getAllClients();
    if (clients != null && clients.size() > 0) {
        writer.printLine(StringUtils.center("Clients", 55) + StringUtils.center("Accounts", 85));
        writer.printLine(StringUtils.repeat("-", 140));
        String columnsNames = String.format("%1s%2s%3s%4s%5s%6s%7s", "id", "e-mail", "name |",
                "id", "created", "login", "password");
        writer.printLine(columnsNames);
        writer.printLine(StringUtils.repeat("=", 140));
        StringBuilder clientInfo = new StringBuilder();
        for (ClientDTO client : clients) {
            clientInfo.append(String.format("%1d%2s%3s |", client.getId(), client.getEmail(),
                    client.getName()));
            writer.printLine(clientInfo);
            clientInfo.delete(0, clientInfo.length());
            List<AccountDTO> accounts = client.getAccounts();
            if (accounts != null && accounts.size() > 0) {
                for (AccountDTO ac : accounts) {
                    clientInfo.append(String.format("%1d%2s%3s%4s", ac.getId(), ac.getCreated(), ac.getLogin(),
                            ac.getPassword()));
                    clientInfo.setCharAt(56, '|');
                    writer.printLine(clientInfo);
                    clientInfo.delete(0, clientInfo.length());
                }
            }
            clientInfo.delete(0, clientInfo.length());
            writer.printLine(StringUtils.repeat("-", 140));
        }
    } else {
        writer.printLine("No data to display.");
        log.info("No data to display.");
    }
}

A StringBuildercount 设置为 0 而不是重新分配或清除其内部 char[] 数组。这是过程中某处发生的情况,不是任何损坏或不一致。

您通过 ArgumentCaptor 捕获了一些 StringBuilder 对象。俘虏所做的是,它接受提供给 System.out.println(Object) 调用的参数。在该调用中,toString() 方法在对象上隐式调用,但捕获采用 StringBuilder 本身,然后将其清空。正如@Sormuras 提到的,在构建器上调用的 delete 方法是导致零计数的原因。

解决方案?好吧,也许在 ClientView.displayAllClientsInfo() 中显式调用 toString(),从 StringBuilder 中创建一个实际的 String。 String builder 只是用来构建一个 String,问题是多亏了 captor,在它的生命周期几乎已经结束后你仍然在使用它。

此外,在 displayAllClientsInfo 方法中使用 StringBuilder 几乎没有意义,你几乎没有使用它的任何功能,我只坚持使用 String.format .

看起来您的 clientInfo.delete(0, clientInfo.length()); 之一在构建预期的字符串内容后将长度设置为 0。