Skip to content

Instantly share code, notes, and snippets.

@kasramp
Last active October 5, 2018 05:51
Show Gist options
  • Save kasramp/3b7c43d0ce66ddeab1548328ce5725cd to your computer and use it in GitHub Desktop.
Save kasramp/3b7c43d0ce66ddeab1548328ce5725cd to your computer and use it in GitHub Desktop.
A test class written in JUnit4
import com.madadipouya.junit5.service.StudentService;
import com.madadipouya.junit5.service.CourseService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class StudentServiceJUnit4Test {
@InjectMocks
private StudentService studentService;
@Mock
private CourseService courseService;
@Test
public void testCalculatingCGPA() {
long studentId = 1L
when(courseService.getAllCoursesByStudentId(anyLong())).thenReturn(stubCourses());
long cGPA = studentService.getCGPA(studentId);
verify(courseService, times(1)).getAllCoursesByStudentId(anyLong());
assertEquals("3.4", cGPA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment