Java Mockito ArgumentCaptor with ByteBuffer在客户端服务器应用程序中返回与Mockito.verify相同的ByteBuffer

Java Mockito ArgumentCaptor with ByteBuffer在客户端服务器应用程序中返回与Mockito.verify相同的ByteBuffer,java,maven,junit,mockito,client-server,Java,Maven,Junit,Mockito,Client Server,我有一个与NIO选择器连接的客户机-服务器应用程序 我正在测试客户端。为此,我对服务器进行Mockito.spy,并使用服务器的方法answer执行Mockito.verify,以捕获服务器接收到的缓冲区 我想检查传递给函数answer的每个缓冲区。为此,我做了以下工作: Mockito.verify(serverSpy, Mockito.times(3)).answer(byteBufferArgumentCaptor.capture()); for (ByteBuffer buf: byt

我有一个与NIO选择器连接的客户机-服务器应用程序

我正在测试客户端。为此,我对服务器进行Mockito.spy,并使用服务器的方法answer执行Mockito.verify,以捕获服务器接收到的缓冲区

我想检查传递给函数answer的每个缓冲区。为此,我做了以下工作:

Mockito.verify(serverSpy, Mockito.times(3)).answer(byteBufferArgumentCaptor.capture());

for (ByteBuffer buf: byteBufferArgumentCaptor.getAllValues()) {
   System.out.println(getObjectFromBuffer(buf).i);
   //getObjectFromBuffer is a helper, i is a field
}
> Connected to the server 
> Client connected 
> 3
> 3
> 3
问题是,除了byteBufferArgumentCaptor的所有值都等于上次发送的对象这一点之外,其他一切都正常工作

我将附上最小可复制代码示例

public class Client {
    SocketChannel clientSocketChannel;
    ByteBuffer buffer;

    public Client(String hostname, int port){
        try {
            clientSocketChannel = SocketChannel.open(new InetSocketAddress(hostname, port));
            buffer = ByteBuffer.allocate(1024);
            System.out.println("Connected to the server");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void send(Object o) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.flush();

        clientSocketChannel.write(ByteBuffer.wrap(bos.toByteArray()));
        buffer.clear();
        clientSocketChannel.read(buffer);
    }
}
我使用以下maven配置运行它(如果它在其他版本的Java/Mockito上工作):


15
15
org.junit.jupiter
一切正常运转的地方。但是,它将ByteBuffer与原语一起使用

我可能错误地使用了缓冲区,但我找不到我做错了什么

public class Obj implements Serializable {
    int i;
    public Obj(Obj toCopy){ this.i = toCopy.i;}
    public Obj(int i){ this.i = i;}
}
public class ClientTest {
    final String hostname = "localhost";
    final int port = 4040;

    private Obj getObjectFromBuffer(ByteBuffer buffer) throws IOException, ClassNotFoundException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer.array());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Object object = objectInputStream.readObject();
        return new Obj((Obj) object);
    }

    @Test
    void test() throws IOException, ClassNotFoundException {
        Obj obj1 = new Obj(1);
        Obj obj2 = new Obj(2);
        Obj obj3 = new Obj(3);

        ArgumentCaptor<ByteBuffer> byteBufferArgumentCaptor = ArgumentCaptor.forClass(ByteBuffer.class);

        Server serverSpy = Mockito.spy(new Server(hostname,port));
        serverSpy.run();

        Client client = new Client(hostname,port);

        client.send(obj1);
        client.send(obj2);
        client.send(obj3);

        Mockito.verify(serverSpy, Mockito.times(3)).answer(byteBufferArgumentCaptor.capture());

        for (ByteBuffer buf: byteBufferArgumentCaptor.getAllValues()) {
            System.out.println(getObjectFromBuffer(buf).i);
        }

    }
}
> Connected to the server 
> Client connected 
> 3
> 3
> 3
<properties>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.5.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jgroups</groupId>
        <artifactId>jgroups</artifactId>
        <version>3.6.8.Final</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>