Skip to main content
Code Review

Return to Question

make title describe what code does instead of concerns about it, per site standard - for more info see https://codereview.stackexchange.com/help/how-to-ask
Link

Unit testing, am I doing it wrong? Video Service class

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> {
}
Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1239340791639785472
Source Link
Patrick
  • 341
  • 3
  • 9

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> {
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /