Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created December 4, 2021 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/ccb48a312193655f2dc45de2dd4fd3ef to your computer and use it in GitHub Desktop.
Save cuongld2/ccb48a312193655f2dc45de2dd4fd3ef to your computer and use it in GitHub Desktop.
Unit test examle
package donald.apiwithspringboot;
import donald.apiwithspringboot.controller.MovieController;
import donald.apiwithspringboot.model.Movie;
import donald.apiwithspringboot.repository.MovieRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Optional;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MovieTest {
@MockBean(name = "movieRepository")
MovieRepository movieRepository;
@Autowired
private MovieController movieController;
@Test
public void blogById () {
Movie movie = new Movie();
movie.setTitle("Testing");
movie.setContent("A movie about Testing");
movie.setId(1);
movie.setAuthor("Donald");
Mockito.when(movieRepository.findById(movie.getId())).thenReturn(Optional.of(movie));
Optional<Movie> blog_service_abc = Optional.ofNullable(movieController.show("1"));
assertThat(movie.getTitle()).isEqualTo(blog_service_abc.get().getTitle());
assertTrue(movie.getContent().matches(".*movie about.*"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment