0

So I break down my code a bit to make it more general and also easier for others to understand with the similar issue

this is my main code:

protected void methodA(String name) {
        Invocation.Builder requestBuilder = webTarget.request();
        requestBuilder.header(HttpHeaders.AUTHORIZATION, authent.getPassword());

            response = request.invoke();

            if (response.equals("unsuccessfull")) {
                log.warn("warning blabla: {} ({})"); 
            } else {
                log.info("info blabla {}");
            }
        } 
    }
}

while my test code looks like this:

@Test
public void testMethodA() throws Exception {            
    final String name = "testName";

    this.subject.methodA(name);

    Authent authent = Mockito.mock(Authent.class);

    when(authent.getPassword()).thenReturn("testPW");
    assertEquals(1, logger.infos.size());

}

as I said the code is more complex I broke it down and made it shorter..... hope still it is readable.

My problem is not that my when().thenReturn() doesn't work and therefore my code doesn't proceed further.... I guess my mocking doesn't work properly for some reason.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Nali
  • 157
  • 2
  • 5
  • 22

2 Answers2

5

You test the methodA() method but you mock the Authent class and record a behavior for it after invoking the tested method :

this.subject.methodA(name);
Authent authent = Mockito.mock(Authent.class);
when(authent.getPassword()).thenReturn("testPW");

This is helpless as the method to test was already invoked.
It should be done in the reverse way :

Authent authent = Mockito.mock(Authent.class);
when(authent.getPassword()).thenReturn("testPW");
this.subject.methodA(name);

Besides, mocking an object is first step.
If the mocked object is not associated to the object under test, it will have no effect on the object under test.

You should do something in this way :

Authent authent = Mockito.mock(Authent.class);
// record behavior for the mock
when(authent.getPassword()).thenReturn("testPW");

// create the object under test with the mock
this.subject = new Subject(authent);

// call your method to test
this.subject.methodA(name);

// do your assertions
...
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • here aswell I get a null pointer exception because authent is "null" even though I implemented it as you did here – Nali Jun 16 '17 at 08:57
  • Are you sure that the `Subject(Authent authent);` constructor assigns the `authent` parameter to its `Authent authent` field ? – davidxxx Jun 16 '17 at 09:00
  • yes I have the `@Autowired Authent authent;` in the top of my main class and the constructor which has the following code `this.authent=authent;` – Nali Jun 16 '17 at 09:14
  • So you have probably the NullPointerException for another field. Ensure that this field is valued (naturally instantiated if you don't need to mock it or mocked if you need to) – davidxxx Jun 16 '17 at 09:30
1

You have to mock before you invoke the method under test. Also you have to inject that mock into your class under test.

With added structural comments this would look like:

@Test
public void testMethodA() throws Exception {            
    // Arrange
    final String name = "testName";
    Authent authentMock = Mockito.mock(Authent.class);
    when(authentMock.getPassword()).thenReturn("testPW");

    this.subject.setAuthent(authentMock);

    // Act
    this.subject.methodA(name);

    // Assert
    assertEquals(1, logger.infos.size());

}
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63