Reputation: 183
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);
}
}
Upvotes: 2
Views: 2340
Reputation: 19070
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);
}
Upvotes: 2
Reputation: 5394
Change the setUp()
method to:
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
Upvotes: 2