I wrote my first unit testing code on a service class using Mockito. The code looks like this:
// Test Class
public class TestVideoService {
private VideoService videoService;
@Mock private VideoRepository videoRepository;
@Rule public MockitoRule mRule = MockitoJUnit.rule();
@Before
public void setUp(){
this.videoService = new VideoService(videoRepository);
}
@Test
public void testGetAllVideos() throws Exception{
when(videoRepository.findAll()).thenReturn(new ArrayList<Video>());
assertEquals(new ArrayList<Video>(), videoService.getAllVideos(), "tested");
}
}
I also have several questions. I feel like I'm just comparing the same mock values (ArrayList of Video) – what's the point of doing this, or am I just doing it the wrong way?
Some other classes:
@Service
public class VideoService {
@Autowired
private VideoRepository videoRepository;
public VideoService(VideoRepository videoRepository){
this.videoRepository = videoRepository;
}
public List<Video> getAllVideos(){
return videoRepository.findAll();
}
}
@Repository
public interface VideoRepository extends JpaRepository<Video, Long> {
}