简体   繁体   English

如何模拟java.net.NetworkInterface?

[英]How to mock java.net.NetworkInterface?

I am going to test a method that takes a List of java.net.NetworkInterface as argument, and so I should mock the final abstract class or instantiate it. 我将测试一个以java.net.NetworkInterface列表作为参数的方法,因此我应该模拟最终的抽象类或实例化它。 Any idea about doing these? 关于做这些的任何想法?

The method is something like this: 方法是这样的:

public void handleInterfaces(List<NetworkInterface> interfaces){
    for(NetworkInterface interface : interfaces){
        //get interface mac address
        //get interface name
        //doSomething here;
    }
}

Writing a mockito-when for every getter method is kind of ugly, so I think I should write my own version of this POJO class with a constructor. 为每个getter方法编写一个mockito-when有点难看,所以我想我应该用构造函数编写我自己的这个POJO类的版本。 Before doing that, I am wondering is there a better scheme to just do something like this: 在这之前,我想知道是否有更好的方案来做这样的事情:

NetworkInterface mockedInterface = instantiateTheInterface("eth1",192.168.1.1,theMacAddress);

I stick with the rule "don't use powermockito ever", so I just implemented a wrapper class and I think its the cleanest way: 我坚持“永远不要使用powermockito”的规则,所以我只是实现了一个包装类,我认为它是最干净的方式:

public class NetworkInterfaceWrapper{
    private NetworkInterface networkInterface;
    public NetworkInterfaceWrapper(NetworkInterface networkInterface){
        this.networkInterface = networkInterface;
    }
    public String getName(){
        return networkInterface.getName();
    }
    ...and so on for all Getters i've used from NetworkInterface
}

Final Solution It turns out that there is another annoying Object in NetworkInterface, called InterfaceAddress which i should write another wrapper for that! 最终解决方案事实证明在NetworkInterface中还有另一个令人讨厌的对象,称为InterfaceAddress,我应该为此编写另一个包装器! So i am going to use shell commands to retrieve the mac address, net mask and interface name and gateway of host and i don't want to use NetworkInterface ever because with all of these restrictions they are just suggesting "You are not allowed to touch this"! 所以我将使用shell命令来检索主机的mac地址,网络掩码和接口名称以及网关,我不想使用NetworkInterface因为所有这些限制他们只是建议“你不允许触摸这个”! PS: I wonder why Oracle guys are obsessed with final abstract, i know that they know more than i do, but in this particular case of NetworkInterface, why final abstract? PS:我想知道为什么甲骨文家伙最终会痴迷于最终摘要,我知道他们比我知道的更多,但在这个特殊的NetworkInterface案例中,为什么最终抽象? using a single comprehensive constructor would make the class, immutable too 使用单个综合构造函数会使类成为不可变的

You can use PowerMockito to mock java standard library final class. 您可以使用PowerMockito来模拟java标准库最终类。

For example; 例如;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ NetworkInterface.class })
public class NetworkInterfaceMocks {

@Test
public void sameClassSuccess() throws Exception {
    final NetworkInterface mockInterface = PowerMockito.mock(NetworkInterface.class);

    when(mockInterface.isUp()).thenReturn(true);
    assertTrue(mockInterface.isUp());
}

@Test
@PrepareForTest(OtherClass.class)
public void differentClassSuccess() throws Exception {
    final NetworkInterface mockInterface = PowerMockito.mock(NetworkInterface.class);

    when(mockInterface.isUp()).thenReturn(true);
    assertTrue(new OtherClass().isUp(mockInterface));
}

In my opinion it should be used only in very rare and non-avoidable cases. 在我看来,它应该只用于非常罕见和不可避免的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM