简体   繁体   中英

JUNIT - Null pointer Exception while calling findAll in spring Data JPA

I am new to Junits and Mockito, I am writing a Unit test class to test my service class CourseService.java which is calling findAll( ) method of CourseRepository.class which implements CrudRepository<Topics,Long>

Service Class

@Service
public class CourseService {

    @Autowired
    CourseRepository courseRepository;

    public void setCourseRepository(CourseRepository courseRepository) {
        this.courseRepository = courseRepository;
    }

    public Boolean getAllTopics() {

        ArrayList<Topics> topicList=(ArrayList<Topics>) courseRepository.findAll();
        if(topicList.isEmpty())
        {
            return false;
        }
        return true;
    }
}

Repository class

public interface CourseRepository extends CrudRepository<Topics,Long>{

}

Domain class

@Entity
@Table(name="Book")
public class Topics {

    @Id
    @Column(name="Topicid")
    private long topicId;

    @Column(name="Topictitle",nullable=false)
    private String topicTitle;

    @Column(name="Topicauthor",nullable=false)
    private String topicAuthor;

    public long getTopicId() {
        return topicId;
    }
    public void setTopicId(long topicId) {
        this.topicId = topicId;
    }

    public String getTopicTitle() {
        return topicTitle;
    }
    public void setTopicTitle(String topicTitle) {
        this.topicTitle = topicTitle;
    }
    public String getTopicAuthor() {
        return topicAuthor;
    }
    public void setTopicAuthor(String topicAuthor) {
        this.topicAuthor = topicAuthor;
    }
    public Topics(long topicId, String topicTitle, String topicAuthor) {
        super();
        this.topicId = topicId;
        this.topicTitle = topicTitle;
        this.topicAuthor = topicAuthor;
    }
}

Following is the Junit class I have written but courseRepository is getting initialized to NULL and hence I am getting NullPointerException .

public class CourseServiceTest {

    @Mock
    private CourseRepository courseRepository;

    @InjectMocks
    private CourseService courseService;

    Topics topics;

    @Mock
    private Iterable<Topics> topicsList;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(CourseServiceTest.class);
    }
    @Test
    public void test_Get_Topic_Details() {

        List<Topics> topics = new ArrayList<Topics>();
        Mockito.when(courseRepository.findAll()).thenReturn(topics);
        boolean result=courseService.getAllTopics();
        assertTrue(result);
    }
}

Change the setUp() method to:

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

Probably you are dealing with some problem on the framework to make the mocked class be injected by the framework.

I recommend to use Constructor Injection , so you don't need to rely on the reflection and @Inject / @Mock annotations to make this work:

@Service
public class CourseService {

    private final CourseRepository courseRepository;

    // @Autowired annotation is optional when using constructor injection
    CourseService (CourseRepository courseRepository) {
        this.courseRepository = courseRepository;
    }

    // .... code

}

The test:

@Test
public void test_Get_Topic_Details() {

    List<Topics> topics = new ArrayList<Topics>();
    Mockito.when(courseRepository.findAll()).thenReturn(topics);

    CourseService courseService = new CourseService(courseRepository);
    boolean result = courseService.getAllTopics();
    assertTrue(result);
}

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