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

时间:2017-10-25 09:11:30

标签: java mockito stringbuilder junit5

我为方法编写测试,通过某些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个类型对象组成:Strings和StringBuilders。然后我想在测试目的中将所有这些对象转换为一个字符串。但是outputList中的所有StringBuilders都已损坏 - 它们有count = 0.因此,当我尝试转换这些StringBuilders时,我得到了空字符串。 见下面的测试代码 - 我留下评论哪里有问题。

问题:

  • 为什么StringBuilders在这里被破坏?如果原因是“ StringBuilder的实例不安全,可供多个线程使用。”(source) - 请解释它在这种情况下的影响,我不手动创建线程......
  • 如何处理?

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("%1$5s%2$25s%3$27s%4$3s%5$30s%6$25s%7$25s", "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("%1$5d%2$25s%3$25s |", 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("%1$60d%2$30s%3$25s%4$25s", 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.");
    }
}

problem

2 个答案:

答案 0 :(得分:3)

StringBuildercount设置为0,而不是重新分配或清除其内部char[]数组。这就是在这个过程中发生的事情,它不是任何腐败或不一致。

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

解决方案?好吧,也许可以在toString()中明确致电ClientView.displayAllClientsInfo(),从String中取出实际的StringBuilder。字符串构建器仅用于构建String,问题在于,由于捕获器,在生命周期已经结束之后,您仍然在使用它。

此外,在StringBuilder方法中使用displayAllClientsInfo几乎毫无意义,你根本不使用任何功能,我只是坚持String.format。< / p>

答案 1 :(得分:1)

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