java - Mocking not working for Socket class -


Whenever a new socket ("server IP", port NIM) is called, I want to return a defined socket instance Yes, my code looks like this (using SimpleMock)

  Public Soft Socket Mock Socket; Public static object [] logic = {"test client", 8443}; ............................................... ....... ........................................ Mock socket = new socket ("20.206.214.76" , 8080); PowerMock.createMock (socket class, logic); ExpectNew (Socket.class, Logic) .andReturn (mockSocket);  

This is giving me a compilation error ->

  PowerMock.createMock (socket class, argument);  

There is an error:

  "type org.easymock.ConstructorArgs can not be solved. It is indirectly referenced from the class squared"  

Can anyone help you?

You do not have the necessary Isaac Monkeys in your class looking for the compiler class org.easymock.ConstructorArgs And it can not be found.

Once you have found the right jar in your class, however, you should work on the general understanding and joke of Java.

  // Here you create a * real * socket and assign it to 'MOSCOCKET' Mock socket = new socket ("20.206.214.76", 8080); // Here you make a fake socket, but do not apply it anyhow / 'logic' probably does not do what you expect. PowerMock.createMock (socket class, logic); // Here you have a real socket to stop the call for your test case 'New socket ()' and // Return mock socket (which we will see above). ExpectNew (Socket.class, Logic) .andReturn (mockSocket);  

What you really need to do not create a real socket, assign a fake socket made by you mockSocket , and Use it in your hopes. Your test class needs to be annotated with @PrepareForTest , or expectation () will not work.

  @RunWith (PowerMockRunner.class) @PrepareForTest (MyClass.class) Public Classroom MyClassTest {// ... @Test Public Zero TestSocket () {// Create Mock Object Socket MockSockets = PowerMock .cent mock (socket.class); // tell the test runner that newer than 'new socket' (socket class, arguments). Andrew (Mock Sock); MyClass objectUnderTest = New MyClass (); // execute objects to work // Because of @PefectForrestTest and expectant (), when MyClass // hits 'new socket (...)' instead of fake // objectUnderTest}}   

Comments