Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito:How to mock method called inside another method

I am new to mockito and I am using mockito to test method which is calling another method and called method returns string. I tried but I am unable write test. Please help

public class MyClass {
  protected String processIncominData(String input) {
    String request = ...;
    ...
    String response = forwardRequest(request);
    ...
    return response;
  }

  public String forwardRequest(String requestToSocket) {
   String hostname = socketServerName;
    int port = socketServerPort;
    String responseLine=null;
    Socket clientSocket = null;  
    PrintStream outs=null;

    BufferedReader is = null;
    BufferedWriter bwriter=null;

    try {
        clientSocket = new Socket(hostname, port);
        outs=new PrintStream(clientSocket.getOutputStream());
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        bwriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

    } catch (UnknownHostException e) {
        LOGGER.error("Don't know about host: " + hostname + e.getMessage());
    } catch (IOException e) {
        LOGGER.error("Couldn't get I/O for the connection to: " + hostname + e.getMessage());
    }

    if (clientSocket == null || outs == null || is == null) {
        LOGGER.error("Something is wrong. One variable is null.");
    }
    try {
        while ( true ) {
            StringBuilder sb = new StringBuilder();
            sb.append(requestToSocket);

            String  request = sb.toString().trim();
            bwriter.write(request);
            bwriter.write("\r\n");
            bwriter.flush();
            responseLine = is.readLine();
            LOGGER.info("Socket returns : " + responseLine);
            //outs.println(responseLine);
            bwriter.close();
        }
    } catch (UnknownHostException e) {
        LOGGER.error("Trying to connect to unknown host: "+ e.getMessage());
    } catch (IOException e) {
        LOGGER.error("IOException:  "+ e.getMessage());
    }
    finally{
        try {
            outs.close();
            is.close();
            clientSocket.close(); 
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return responseLine;
  }
}

I want to mock xml response here to test processIncomingData method..This response is coming from socket server and I am sending request by socket client. I think socket will not matter if I can mock xmlResponse from socket. Please give any helpful answer

like image 986
Lakshya Avatar asked Nov 25 '14 12:11

Lakshya


People also ask

How do you mock a method call inside another method in the same class Junit?

We can mock runInGround(String location) method inside the PersonTest class as shown below. Instead of using mock(class) here we need to use Mockito. spy() to mock the same class we are testing. Then we can mock the method we want as follows.

Does Mockito mock call real method?

Mockito allows us to partially mock an object. This means that we can create a mock object and still be able to call a real method on it. To call a real method on a mocked object we use Mockito's thenCallRealMethod().


1 Answers

Now that you have posted it, the answer to how to mock it is here.

testing-java-sockets

like image 175
John B Avatar answered Nov 05 '22 18:11

John B