最终类模拟不返回预期值

时间:2014-02-18 21:20:09

标签: java unit-testing mockito powermock

我正在尝试编写一个将DatagramPacket带入其构造函数的类的单元测试。我已经将测试(第一个)和被测试的课程(第二个)包含在下面。

问题在于我获得的输出都不是我期望的基于我的when()。thenReturn调用。在我的测试中,我有三个打印语句(在第一个代码块中查看test()方法的底部)。它的输出是

IP:0.0.0.0,端口:0,数据:

我想要的输出是:

IP:90.35.52.52,端口:25200,数据:heartbeat ^ 0a522

有没有人碰巧知道为什么我没有收到预期的输出?

谢谢!

UDPPacket_TEST.java

import static org.mockito.Mockito.*;

import java.net.DatagramPacket;
import java.net.InetAddress;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(DatagramPacket.class)
public class UDPPacket_TEST {

    DatagramPacket mDatagramPacket;
    UDPPacket mPacket;

    @Before
    public void setup() {
        PowerMockito.mockStatic(DatagramPacket.class);
        mDatagramPacket = PowerMockito.mock(DatagramPacket.class);
        InetAddress addRet = mock(InetAddress.class);
        when(addRet.getHostAddress()).thenReturn("90.35.52.52");
        PowerMockito.when(mDatagramPacket.getPort()).thenReturn(25200);
    }

    @Test
    public void test() {
        byte[] packetData = "heartbeat^0a552".getBytes();
        PowerMockito.when(mDatagramPacket.getData()).thenReturn(packetData);

        mPacket = new UDPPacket(mDatagramPacket);

        System.out.println(String.format("IP: %s, Port: %d, Data: %s", mPacket.getIP(), mPacket.getPort(), mPacket.getData()));
    }

}

UDPPacket.java

public class UDPPacket {

    String mIP;
    int mPort;
    String mData;

    public UDPPacket(DatagramPacket receivedPacket) {
        //Grab the information out
        mData = new String(receivedPacket.getData());
        mIP = receivedPacket.getAddress().getHostAddress();
        mPort = receivedPacket.getPort();
    }

    public String getIP() {
        return mIP;
    }

    public int getPort() {
        return mPort;
    }

    public String getData() {
        return mData;
    }

}

1 个答案:

答案 0 :(得分:0)

不是我喜欢测试方法(但我有时理解这种必要性......)。 深入了解PowerMockito的源代码,您将看到您根本没有创建DatagramPacket的“模拟”实例,而是接收“常规”实例。

但是,您可以使用Whitebox设置实例的字段:

        Whitebox.setInternalState(mDatagramPacket, "buf", packetData);

“修正”代码......

<强> UDPPacket_TEST.java

@RunWith(PowerMockRunner.class)
@PrepareForTest(DatagramPacket.class)
public class UDPPacket_TEST {

    DatagramPacket mDatagramPacket;
    UDPPacket mPacket;
    InetAddress addRet;

    @Before
    public void setup() {
        mDatagramPacket = PowerMockito.mock(DatagramPacket.class);
        addRet = mock(InetAddress.class);
        when(addRet.getHostAddress()).thenReturn("90.35.52.52");
        PowerMockito.when(mDatagramPacket.getPort()).thenReturn(25200);
    }

    @Test
    public void test() {
        byte[] packetData = "heartbeat^0a552".getBytes();
        Whitebox.setInternalState(mDatagramPacket, "buf", packetData);
        Whitebox.setInternalState(mDatagramPacket, "address", addRet);
        Whitebox.setInternalState(mDatagramPacket, "port", 25200);
        PowerMockito.when(mDatagramPacket.getData()).thenReturn(packetData);

        mPacket = new UDPPacket(mDatagramPacket);

        System.out.println(String.format("IP: %s, Port: %d, Data: %s", mPacket.getIP(), mPacket.getPort(), mPacket.getData()));
    }

}

生成您想要的输出:

IP: 90.35.52.52, Port: 25200, Data: heartbeat^0a552