1

I have this below piece of code which I'm trying to mock using power mock and mockito, but it is proving to be very difficult. Can someone explain on mocking the below code.

HttpClient httpClient = HttpClient.newBuilder().authenticator(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abcd", "efgh".toCharArray());
        }
    }).connectTimeout(Duration.ofSeconds(10)).build();

    HttpRequest request = HttpRequest.newBuilder().POST(HttpRequest.BodyPublishers.noBody()).uri(URI.create(
            "www.xyz.com"))
            .build();
daniel
  • 2,665
  • 1
  • 8
  • 18
  • How about `mock(HttpClient.class)` (or the `@Mock` equivalent)? You may just have to make `httpClient` an instance variable in your class so that it can be injected during tests rather than built during method invocations run by your tests. – ernest_k Oct 14 '21 at 04:58
  • The openjdk tests on github have a test that demonstrate how to mock an HttpClient so that the mock implementation returns canned responses, without doing any network access: https://github.com/openjdk/jdk/tree/master/test/jdk/java/net/httpclient/offline See OfflineTesting.java. It may not be exactly what you are looking for but it might give you some ideas. – daniel Oct 14 '21 at 15:47

1 Answers1

0

Mocking is about creating a Fake Object to represent a dependency in the class Under Test. Suppose we have a dependency relationship between two classes : ClassA and ClassB, as ClassA depends on ClassB to perform an action. While testing ClassA (your class Under Test), you will have to mock ClassB (your Fake Object).

What mocking really allows you to do, is to set a control on how you want the methods in ClassB to behave while your are testing ClassA, and in general limits the dependency with real objects (database, network connection, API etc...).

An example:

class ClassA {

    ClassB b;

    public int doSomethingDependingOnClassB() {
        return b.iAmHelpingClassA();
    }
}

Your test will look something like this :

class ClassATest {

    @Mock
    private ClassB classBMock;

    @InjectMocks
    private ClassA underTest;

    @Test
    void testDoSomethingDependingOnClassB() {
        when(classBMock.iAmHelpingClassA()).thenReturn(20);
        
        int excepectedResult = 20;
        assertEquals(excepectedResult, underTest.doSomethingDependingOnClassB());
    }
}

Mocking Frameworks will provide you some methods or annotations to help you setup your test scenario.

Coming to your case, you will have to mock the HttpClient and define what you want the call of your methods will return (here, newBuilder() and authenticator()).

As you said mocking can be a vast and difficult topic, but as everything, you have understand the basic principle. Your friends here are Google or Youtube. There are a lots of good tutorials that can help understand mocking in unit testing.

Harry Coder
  • 2,429
  • 2
  • 28
  • 32