0
public abstract class A {

    public String method1( String urlString ) {
        HTTP HttpURLConnection con = getConnection(url);

        .... I use con.getInpurStream() to get the data and return it...

    }
    public HttpURLConnection getConnection(URL url) {
        URL url = new URL(urlString);
        return (HttpURLConnection) url.openConnection();
    }
}


public class B extends A {
    public String method2(String urlString) {
        return method1(urlString);
    }
}

Im trying to write unit test for Class B method2 using JUNIT and Mockito.

I tired something like this :

public class testB {
    String urlString = "someValid.url";
    String dummyRes = "dummy Response";
    InputStream res = new ByteArrayInputStream(dummyRes.getBytes());

    B mockB = Mockito.mock(B.class);
    HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
    Mockito.when(connection.getInputStream()).thenReturn(res);
    Mockito.when(mockB.getConnection(urlString)).thenRetrun(connection);

    assertEquals("dummy Response", mockB.method2("someValid.url");

}

I keep getting either the real time data from that url instead of dummyRes.

Please help /\

Edit1:

Removed the static methods. And im still not able to mock the getConnection() method to return the mocked connection.

Edit2:

Moved making URL object to getConnection method to avoid making a new URL so that I can mock it and made changes in the test for that. Still picking up real time values.

  • The `URL` you're mocking in your test isn't actually being used. Instead, you're creating a new `URL` from the provided `String`. Hence why it's doing a live connection. – Dan W May 28 '20 at 15:04

2 Answers2

1

It seems that there should be something like this

Mockito.when(mockB.getConnection(Mockito.eq("someValid.url" ))).thenReturn(connection);
Alex
  • 706
  • 7
  • 16
1

The static methods cannot be mocked directly , for mocking static methods you need to use PowerMock on top of Mockito.

One more option could be using Jmockit Framework. This can be used instead of using two frameworks(Mockito and PowerMock).

Here is the reference , why mocking of static methods won't work with Mockito -

Mocking static methods with Mockito.

[update] : for the mock issue of connection,

instead of


    Mockito.when(mockB.getConnection(tempURL)).thenRetrun(connection);

try using


    Mockito.when(mockB.getConnection(any())).thenRetrun(connection);

If you don't wish to pass any, Alternative could be mocking URL class along with the method openConnection as well.

Allabakash
  • 1,969
  • 1
  • 9
  • 15
  • Thanks @Allabakash. I refactored such that I dont use statics anymore. But now I get only real time data from the url rather than the mocked connection data. – Anirudh Ghanta May 28 '20 at 14:54
  • @AnirudhGhanta, That is because , tempURL is different object compare to whats get created inside your parent class. Instead of passing tempURL, try passing any() while mocking it, that should fix the issue. I have updated the answer as well. – Allabakash May 29 '20 at 08:16