Testing Services

A Service with business logic needs to be tested to ensure it functions correctly. In order to do this, a Service like the following can be used:

@RunWith(MockitoJUnitRunner::class)
@ActiveProfiles("dev")
class MovieServiceTest {

@Mock
lateinit var movieRepository: MovieRepository;

lateinit var movieService: MovieService;

In the preceding test case for MovieService , MovieRepository is annotated with @Mock. During Service, test mocking is done using the Mockito library just to mock repository method invocations and verify the correct invocation. Consider the following code:

    @Before
fun setup() {
movieService = MovieService(movieRepository);
}

The preceding code is used to create an instance of MovieService with a mocked MovieRepository. Consider the following code:

    @Test
fun `Saving a Movie - Happy Path`() {
// Given
var expected = getMovie();
`when`(movieRepository.save(expected)).
thenReturn(Mono.just(expected));

// When
val actual = movieService.save(expected);

// Then
actual.subscribe({movie ->
assertThat(movie).isEqualTo(expected)});
verify(movieRepository, times(1)).save(expected);
}

The preceding code snippet tests the Saving a Movie scenario's happy path:

    @Test
fun `Find a Movie by Id - Happy Path`() {
// Given
var expected = getMovie();
`when`(movieRepository.findById(1)).
thenReturn(Mono.just(expected));

// When
val actual = movieService.findOne(1);

// Then
actual.subscribe({movie ->
assertThat(movie).isEqualTo(expected)});
verify(movieRepository, times(1)).findById(1);
}

The preceding code snippet tests the Find a Movie by Id scenario's happy path:

    @Test
fun `Find a Movie by Id - Failure Path`() {
// Given
var expected = getMovie();
`when`(movieRepository.findById(1)).thenReturn(Mono.empty());

// When
val actual = movieService.findOne(1);

// Then
actual.doOnError({t ->
assertThat(t).isInstanceOf(MovieNotFoundException::
class.java)}).
subscribe();
verify(movieRepository, times(1)).findById(1);
}

The preceding code snippet tests the Find a Movie by Id scenario's failure path:

    @Test
fun `Rate a Movie - Happy Path`() {
// Given
var expected = getMovie();
`when`(movieRepository.findById(1)).
thenReturn(Mono.just(expected));
`when`(movieRepository.save(expected)).
thenReturn(Mono.just(expected));

// When
var actual = movieService.rate(1, "Great", 5);

// Then
actual.subscribe({movie ->
assertThat(movie.ratings).hasSize(1)});
verify(movieRepository, times(1)).findById(1);
verify(movieRepository, times(1)).save(expected);
}

The preceding code snippet tests the Rate a Movie scenario's happy path:

    @Test
fun `Rate a Movie - Failure Path`() {
// Given
`when`(movieRepository.findById(1)).thenReturn(Mono.empty());

// When
var actual = movieService.rate(1, "Great", 5);

// Then
actual.doOnError({t ->
assertThat(t).isInstanceOf(MovieNotFoundException::
class.java)}).subscribe();
}

The preceding code snippet tests the Rate a Movie scenario's failure path:

    fun getMovie() : Movie {
return Movie(1, "Avengers", 2018, MovieGenre("Action",
"Action"), ArrayList<MovieRating>(), ArrayList<Actor>())
}


}

The preceding code snippet creates mock Movie data for testing.

The following is the service test case for UserService:

@RunWith(MockitoJUnitRunner::class)
@ActiveProfiles("dev")
class UserServiceTest {

@Mock
lateinit var userRepository: UserRepository;

lateinit var userService: UserService;

@Before
fun setup() {
this.userService = UserService(userRepository);
}

@Test
fun `Saving a User - Happy Path`() {
// Given
var user : User = getUser();
`when`(userRepository.save(user)).thenReturn(Mono.just(user));

// When
var actual = userService.save(user);

// Then
actual.subscribe({u -> assertThat(user).isEqualTo(u)});
verify(userRepository, times(1)).save(user);
}

@Test
fun `Find by Username - Happy Path`() {
// Given
var user : User = getUser();
`when`(userRepository.findByUsername("shazin")).
thenReturn(Flux.just(user));

// When
var actual = userService.getByUsername("shazin");

// Then
actual.subscribe({u -> assertThat(user).isEqualTo(u)});
verify(userRepository, times(1)).findByUsername("shazin");
}

fun getUser() : User {
return User(1, "shazin", "password", "USER", "User of Moviee");
}

}

The preceding tests use lazy initialization with the lateinit keyword for variables that are mocked, autowired, or initialized inside the @Before function. This is done because any variable that is not nullable must be initialized either during declaration or in the constructor. 

Also, Kotlin allows descriptive function names for methods, which describe the scenario being tested, enclosed inside two "`".

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.173