- 29.5k
- 16
- 45
- 201
Unit testing, am I doing it wrong? Video Service class
- 1.1k
- 3
- 14
- 27
iI 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 iI'm just comparecomparing the same mock values ArrayList(ArrayList of Video and) – what's the point of doing this, or iam I just dodoing it the wrong way.?
Some others class.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> {
}
i wrote my first unit testing code on a service class using Mockito. The code looks like
// 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 just compare the same mock values ArrayList of Video and what's the point of doing this or i just do it wrong way.
Some others class.
@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> {
}
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> {
}
Unit testing, am I doing it wrong?
i wrote my first unit testing code on a service class using Mockito. The code looks like
// 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 just compare the same mock values ArrayList of Video and what's the point of doing this or i just do it wrong way.
Some others class.
@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> {
}