简体   繁体   中英

Spring & Mockito : Actual Object getting called in Unit Test

This is the class which i am trying to test,

    public class AuthErrorResponseWriter  {

        @Autowired
        TransResponse svcResponse;


        @Override
        public void writeResponse(HttpServletResponse response) {


            //Set the Http status
            response.setStatus(HttpStatus.FORBIDDEN.value());
            svcResponse.setMessage(Constants.AUTHENTICATION_ERROR);
            svcResponse.setStatus(HttpStatus.FORBIDDEN.toString());

            ObjectMapper mapper = new ObjectMapper();

            //Write the response
            try {
                Writer writer = response.getWriter();
                writer.write(mapper.writeValueAsString(svcResponse));
                writer.flush();
                writer.close();
            } catch (IOException ioex) {
                logger.error("Problem producing authentication error http response",ioex);  
            }
    }

}

The unit test code i have written is below,

RunWith(SpringRunner.class)
@WebMvcTest({AuthErrorResponseWriter  .class})
@ComponentScan("com.demo.service")
public class AuthErrorResponseWriterTest {

    @Mock
    HttpServletResponse responseMock;


    @Before 
    public void setUp(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testResponse(){

        TransResponse mockResponse = new TransResponse();

        mockResponse.setMessage(Constants.AUTHENTICATION_ERROR);
        mockResponse.setStatus(HttpStatus.FORBIDDEN.toString());

        AuthErrorResponseWriter   authErrorWriter = new AuthErrorResponseWriter  (); 
        PrintWriter writerMock = mock(PrintWriter.class);

        try {
            when(responseMock.getWriter()).thenReturn(writerMock);
        } catch (IOException ioex) {
            //assertTrue(false);
        }
        authErrorWriter.writeResponse(responseMock);
        verify(responseMock).setStatus(HttpStatus.FORBIDDEN.value());

    }

}

When i execute this Junit, am getting a null pointer exception for

svcResponse.setMessage(Constants.AUTHENTICATION_ERROR);

svcResponse is null, even though i have mocked it.

Please can someone point to me why its not picking up the mock object and looking for the actual.

Also if my writing the Junit is a proper way?

You may want to use Mockito's runner instead of Spring (from what I see, you do not need Spring's context at all):

@RunWith(MockitoJUnitRunner.class)
public class SubscriptionServiceTest {

    @InjectMocks
    private AuthErrorResponseWriter authErrorResponseWriter;

    @Mock
    TransResponse svcResponse;

    @Mock
    HttpServletResponse responseMock;

    ....

    authErrorWriter.writeResponse(responseMock);

svcResponse is null, even though i have mocked it.

No, you haven't mocked it. This is what you are doing in your code:

TransResponse mockResponse = new TransResponse();
mockResponse.setMessage(Constants.AUTHENTICATION_ERROR);
mockResponse.setStatus(HttpStatus.FORBIDDEN.toString());

What you should be doing is something like this:

@Mock
private TransResponse mockResponse;

You'll have to inject the mocks in the Target class like this:

@InjectMocks
private AuthErrorResponseWriter authErrorWriter;

And use, authErrorWriter instead of creating a new instance of the class in your test.

And then you can do something like this:

Mockito.doNothing().when(mockResponse).setMessage(Constants.AUTHENTICATION_ERROR);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM