Search code examples
javaspringspring-bootmavenspring-mvc

Using JUnit Mocked ObjectMapper shows Null. java.lang.NullPointerException: objectMapper is null


First of all, I am new to UnitTest. When I mocked my objectMapper inside of the test. It returns me null. My last testes were same structure. I believe I am using the corrupted junit. Intellij may be confused.

Code ;

public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint {

    ObjectMapper objectMapper;

    public ApiAuthenticationEntryPoint(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException authException) throws IOException, ServletException {
            Map<String, Object> data = new HashMap<>();
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            String value = request.getAttribute("Exception").toString();

            if(value == "key incorrect"){
                data.put("code","1");
                data.put("message","api key is empty or incorrect");
                request.setAttribute("Exception",
                        objectMapper.writeValueAsString(data));
                value = request.getAttribute("Exception").toString();
            }


            response.getWriter().write(value);
        
    }

}

Test class;

@SpringBootTest
public class ApiAuthenticationEntryPointTest {

    private ApiAuthenticationEntryPoint apiAuthenticationEntryPoint;

    @MockBean
    private ObjectMapper objectMapper;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        apiAuthenticationEntryPoint = new ApiAuthenticationEntryPoint(objectMapper);
    }

    @Test
    public void commence() throws Exception {
        AuthenticationException authenticationException = Mockito.mock(AuthenticationException.class);

        HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);

        HttpServletResponse httpServletResponse = Mockito.mock(HttpServletResponse.class);

        Map<String, Object> data = new HashMap<>();
        data.put("code","1");
        data.put("message", "api key is empty or incorrect");

        Mockito.when(objectMapper.writeValueAsString(data)).thenReturn("1");

        apiAuthenticationEntryPoint.commence(httpServletRequest, httpServletResponse, authenticationException);
    }
}

It gives me the following error;

java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(Object)" because "this.objectMapper" is null

Also I should share dependency tree;

[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.3.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.26:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.2.8.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-config:jar:5.3.4.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-core:jar:5.3.4.RELEASE:compile
[INFO] |  \- org.springframework.security:spring-security-web:jar:5.3.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.2:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.2:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.2:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.37:compile
[INFO] |  |  +- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.37:compile
[INFO] |  +- org.springframework:spring-web:jar:5.2.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.8.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:5.3.3:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.8.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:5.2.8.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.2.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:5.2.8.RELEASE:compile
[INFO] +- org.slf4j:slf4j-api:jar:2.0.0-alpha1:compile
[INFO] +- io.jsonwebtoken:jjwt:jar:0.9.1:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.3.3.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.9.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- com.zaxxer:HikariCP:jar:3.4.5:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.2.8.RELEASE:compile
[INFO] |  +- jakarta.transaction:jakarta.transaction-api:jar:1.3.3:compile
[INFO] |  +- jakarta.persistence:jakarta.persistence-api:jar:2.2.3:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.4.20.Final:compile
[INFO] |  |  +- org.javassist:javassist:jar:3.24.0-GA:compile
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.10.14:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  +- org.jboss:jandex:jar:2.1.3.Final:compile
[INFO] |  |  +- org.dom4j:dom4j:jar:2.1.3:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile
[INFO] |  |  \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.3:compile
[INFO] |  |     +- org.glassfish.jaxb:txw2:jar:2.3.3:compile
[INFO] |  |     \- com.sun.istack:istack-commons-runtime:jar:3.0.11:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:5.2.8.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:5.2.8.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:5.2.8.RELEASE:compile
[INFO] +- javax.xml.bind:jaxb-api:jar:2.3.1:compile
[INFO] |  \- javax.activation:javax.activation-api:jar:1.2.0:compile
[INFO] +- javax.mail:mail:jar:1.4.7:compile
[INFO] |  \- javax.activation:activation:jar:1.1:compile
[INFO] +- org.projectlombok:lombok:jar:1.18.12:compile (optional) 
[INFO] +- org.springframework.boot:spring-boot-starter-mail:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:5.2.8.RELEASE:compile
[INFO] |  \- com.sun.mail:jakarta.mail:jar:1.6.5:compile
[INFO] |     \- com.sun.activation:jakarta.activation:jar:1.2.2:compile
[INFO] +- org.mapstruct:mapstruct:jar:1.2.0.Final:compile
[INFO] +- mysql:mysql-connector-java:jar:8.0.21:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-data-redis:jar:2.3.3.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:2.3.3.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-keyvalue:jar:2.3.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-oxm:jar:5.2.8.RELEASE:compile
[INFO] |  \- io.lettuce:lettuce-core:jar:5.3.3.RELEASE:compile
[INFO] |     +- io.netty:netty-common:jar:4.1.51.Final:compile
[INFO] |     +- io.netty:netty-handler:jar:4.1.51.Final:compile
[INFO] |     |  +- io.netty:netty-resolver:jar:4.1.51.Final:compile
[INFO] |     |  +- io.netty:netty-buffer:jar:4.1.51.Final:compile
[INFO] |     |  \- io.netty:netty-codec:jar:4.1.51.Final:compile
[INFO] |     +- io.netty:netty-transport:jar:4.1.51.Final:compile
[INFO] |     \- io.projectreactor:reactor-core:jar:3.3.9.RELEASE:compile
[INFO] |        \- org.reactivestreams:reactive-streams:jar:1.0.3:compile
[INFO] +- redis.clients:jedis:jar:3.3.0:compile
[INFO] |  \- org.apache.commons:commons-pool2:jar:2.8.1:compile
[INFO] +- com.maxmind.geoip2:geoip2:jar:2.15.0:compile
[INFO] |  +- com.maxmind.db:maxmind-db:jar:2.0.0:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.12:compile
[INFO] |  |  \- org.apache.httpcomponents:httpcore:jar:4.4.13:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.14:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-core:jar:2.11.2:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.2.6.RELEASE:compile
[INFO] |  +- org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile
[INFO] |  \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.4.RELEASE:compile
[INFO] +- org.thymeleaf:thymeleaf-spring4:jar:3.0.0.RELEASE:compile
[INFO] |  \- org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile
[INFO] |     +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile
[INFO] |     \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile
[INFO] +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] +- org.hibernate.validator:hibernate-validator:jar:6.1.5.Final:test
[INFO] |  +- jakarta.validation:jakarta.validation-api:jar:2.0.2:test
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO] |  \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] +- org.springframework.boot:spring-boot-test:jar:2.6.3:test
[INFO] |  \- org.springframework.boot:spring-boot:jar:2.3.3.RELEASE:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:2.2:test
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.3.3.RELEASE:test
[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.3.3.RELEASE:test
[INFO]    +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO]    |  \- net.minidev:json-smart:jar:2.3:test
[INFO]    |     \- net.minidev:accessors-smart:jar:1.2:test
[INFO]    |        \- org.ow2.asm:asm:jar:5.0.4:test
[INFO]    +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
[INFO]    |  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
[INFO]    +- org.assertj:assertj-core:jar:3.16.1:test
[INFO]    +- org.hamcrest:hamcrest:jar:2.2:test
[INFO]    +- org.junit.jupiter:junit-jupiter:jar:5.6.2:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test
[INFO]    |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  |  \- org.junit.platform:junit-platform-commons:jar:1.6.2:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-params:jar:5.6.2:test
[INFO]    |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.2:test
[INFO]    +- org.junit.vintage:junit-vintage-engine:jar:5.6.2:test
[INFO]    |  +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO]    |  \- org.junit.platform:junit-platform-engine:jar:1.6.2:test
[INFO]    +- org.mockito:mockito-core:jar:3.3.3:test
[INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.10.14:test
[INFO]    |  \- org.objenesis:objenesis:jar:2.6:test
[INFO]    +- org.mockito:mockito-junit-jupiter:jar:3.3.3:test
[INFO]    +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO]    +- org.springframework:spring-test:jar:5.2.8.RELEASE:test
[INFO]    \- org.xmlunit:xmlunit-core:jar:2.7.0:test
[INFO] ------------------------------------------------------------------------


Solution

  • If anyone comes to this question, you should not be mocking the ObjectMapper, see my question and accepted answer (you can simply instantiate it):

    private ObjectMapper mapper = new ObjectMapper();
    

    Jackson ObjectMapper is null in JUnit 5 Controller Test