Senthil
Senthil

Reputation: 131

How to mock object for @Autowired in junit 5 unit test?

I am doing unit test for my spring boot service class using junit 5. Inside service class i am autowiring object for calling methods in other class. In test case @Mock is not creating object for @Autowired class. Here i am giving my code.

my service class :

@Service
public class BarcodeReaderService {

    @Autowired
    ImageProcessor imageProcessor;

    public String dummy(String name) {
        System.out.println("function call"); //print success
        return imageProcessor.dummy(name);   //Null pointer Exception
    }
    }

my component class :

@Component
public class ImageProcessor {

    public String dummy(String name) {
        // TODO Auto-generated method stub
        System.out.println("function call ImageProcessor"); //Not coming to this line
        return name;
    }
    }

my unit test class:

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import com.inslab.reader.barcodereader.imageprocessing.ImageProcessor;
import com.inslab.reader.barcodereader.service.BarcodeReaderService;

@ExtendWith(MockitoExtension.class)
public class BarcodeReaderServiceTest {

    @InjectMocks
    BarcodeReaderService barcodeReaderService;

    @Mock
    ImageProcessor imageProcessor;


    @Test
    void testDummy() {
        Mockito.when(imageProcessor.dummy("name")).thenReturn("name");
        String name = barcodeReaderService.dummy("name");
        Assertions.assertEquals("name", name);
    }

}

pom.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.inslab.reader</groupId>
    <artifactId>barcodereader</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>barcodereader</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

While running this test i am getting null pointer exception in imageProcessor.dummy(name);

Upvotes: 2

Views: 20362

Answers (3)

Shruti
Shruti

Reputation: 343

Use @InjectMocks for your Service class in the test class, all other @Autowired inside your service class should be @Mock in test class

Upvotes: 0

Senthil
Senthil

Reputation: 131

I added @SpringBootTest at the begining of my class. It worked. Make sure your import are correct for junit 5. here my code,

package com.inslab.reader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import com.google.zxing.Result;
import com.inslab.reader.ImageProcessor;
import com.inslab.reader.IReader;

@SpringBootTest
class BarcodeReaderServiceTest {

    @InjectMocks
    BarcodeReaderService barcodeReaderService;

    @Mock
    ImageProcessor imageProcessor;

    @BeforeEach //instead of @Before have to use @BeforeEach in junit5
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void testDummy() {
        Mockito.when(imageProcessor.dummy("name")).thenReturn("name");
        String name = barcodeReaderService.dummy("name");
        Assertions.assertEquals("name", name);
    }   
}

Upvotes: 2

rieckpil
rieckpil

Reputation: 12021

You have to register the MocktioExtension for your test like:

@ExtendWith(MockitoExtension.class)
public class BarcodeReaderServiceTest {

    @InjectMocks
    BarcodeReaderService barcodeReaderService;

    @Mock
    ImageProcessor imageProcessor;

    @Test
    void testDummy() {
        when(imageProcessor.dummy("name")).thenReturn("YOUR_OUTCOME");
        String name = barcodeReaderService.dummy("name");
        Assertions.assertEquals("YOUR_OUTCOME", name);
    }

}

Also don't use new in the test to create the instance of your class under test and rather use your field variable barcodeReaderService. When you work with Mockito you also have to specify the behavior of your Mock, otherwise, it will always return null.

Upvotes: 5

Related Questions