9

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

6
  • Show how xmlResponse is being generated. You need to use a mock at the line String xmlResponse = ...;. If this is an instance method call, use Mockito to mock the bean (read socket client). If this is a static call, use JMockit to mock the static call. Commented Nov 25, 2014 at 12:42
  • I am reading xml response from socket server. It is in string format like "<response><test>123</test></response>" etc Commented Nov 25, 2014 at 13:17
  • Your unit test should not depend on a socket server. You should be mocking the client. Commented Nov 25, 2014 at 13:28
  • can you please tell how to mock client? Commented Nov 25, 2014 at 13:29
  • Post more code. Specifically, 1) the full line of code that retrieves xmlResponse 2) how MyClass gets the instance of the client it is using. Commented Nov 25, 2014 at 13:33

2 Answers 2

3

You can try the spy method:

MyClass myClass = spy(new MyClass());
when(myClass.forwardRequest("foo")).thenReturn("bar");

then when you call

myClass.processIncominData("baz");

the response will be "bar" (if request is "foo")

PS: When you need to mock class under test it shows some problem with your design.

Sign up to request clarification or add additional context in comments.

11 Comments

But better refactor this to have thous methods in different classes.
Generally considered bad practice to mock the class under test.
@JohnB Yes. It is wy I'm added comment to separate class.
Didn't realize that was a comment on your own post. Might have been better to edit your post to put the expansion in the body.
@JohnB It was not part of answer, just general hint. But I edited my answer.
|
2

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

testing-java-sockets

1 Comment

but I am not using socket in my first method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.