Skip to content

Commit cb1269b

Browse files
author
Nilanchal Panigrahy
committed
Added unit and integration tests
1 parent 1f26a85 commit cb1269b

File tree

15 files changed

+446
-31
lines changed

15 files changed

+446
-31
lines changed
350 KB
Loading

testing-movies-api/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>spring-boot-starter-web</artifactId>
3535
</dependency>
3636

37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-actuator</artifactId>
40+
</dependency>
41+
3742
<dependency>
3843
<groupId>org.springframework.boot</groupId>
3944
<artifactId>spring-boot-starter-test</artifactId>

testing-movies-api/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Testing Spring Boot RESTFul API
2+
3+
Example code that demonstrates how to test RESTFul API in spring boot using Mockito, MockMVC.
4+
5+
#### Movies API Docs:
6+
7+
The Swagger UI page is available at http://localhost:8080/swagger-ui/index.html and the OpenAPI
8+
description is available at http://localhost:8080/api-docs.
9+
10+
![Movies API - Spring Boot](movies-api-swagger.png "Movies API Docs")
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.stacktips.movies.api;
22

33
import com.stacktips.movies.exception.MovieNotFoundException;
4+
import com.stacktips.movies.models.ApiError;
5+
import org.springframework.http.HttpStatus;
46
import org.springframework.http.ResponseEntity;
57
import org.springframework.web.bind.annotation.ControllerAdvice;
68
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseBody;
10+
11+
import java.util.List;
712

813
@ControllerAdvice
914
public class GlobalExceptionHandler {
1015

16+
@ResponseBody
1117
@ExceptionHandler(MovieNotFoundException.class)
12-
ResponseEntity<Void> handleNotFoundException(MovieNotFoundException ex) {
13-
return ResponseEntity.notFound()
14-
.build();
18+
ResponseEntity<List<ApiError>> handleNotFoundException(MovieNotFoundException ex) {
19+
final ApiError apiError = new ApiError(HttpStatus.NOT_FOUND.name(), ex.getMessage());
20+
21+
return new ResponseEntity<>(List.of(apiError), HttpStatus.NOT_FOUND);
1522
}
1623

1724
}

testing-movies-api/src/main/java/com/stacktips/movies/api/MoviesController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MoviesController(MovieService movieService) {
2828
this.movieService = movieService;
2929
}
3030

31-
@GetMapping
31+
@GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
3232
public List<Movie> getMovies() {
3333
return movieService.getMovies();
3434
}

testing-movies-api/src/main/java/com/stacktips/movies/api/SearchController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.http.MediaType;
88
import org.springframework.web.bind.annotation.GetMapping;
99
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
1011
import org.springframework.web.bind.annotation.RestController;
1112

1213
import java.util.List;
@@ -22,10 +23,10 @@ public SearchController(SearchService searchService) {
2223
this.searchService = searchService;
2324
}
2425

25-
// @GetMapping
26-
// public List<Movie> searchByLanguage(@RequestParam(name = "language") String language) {
27-
// return searchService.searchMovieByLanguage(language);
28-
// }
26+
@GetMapping(path = "/language")
27+
public List<Movie> searchByLanguage(@RequestParam(name = "language") String language) {
28+
return searchService.searchMovieByLanguage(language);
29+
}
2930

3031
@GetMapping
3132
public List<Movie> search(SearchRequest searchRequest) {

testing-movies-api/src/main/java/com/stacktips/movies/dto/MovieDto.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.stacktips.movies.dto;
22

33
import com.stacktips.movies.models.ContentRating;
4+
import jakarta.validation.constraints.NotEmpty;
5+
import jakarta.validation.constraints.NotNull;
46

57
import java.util.List;
68

79
public class MovieDto {
810

11+
@NotEmpty
12+
@NotNull
913
private String title;
14+
1015
private String headline;
1116
private String thumbnail;
1217
private String language;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.stacktips.movies.models;
2+
3+
public class ApiError {
4+
5+
String code;
6+
String message;
7+
8+
public ApiError(String code, String message) {
9+
this.code = code;
10+
this.message = message;
11+
}
12+
13+
public String getCode() {
14+
return code;
15+
}
16+
17+
public void setCode(String code) {
18+
this.code = code;
19+
}
20+
21+
public String getMessage() {
22+
return message;
23+
}
24+
25+
public void setMessage(String message) {
26+
this.message = message;
27+
}
28+
}
29+

testing-movies-api/src/main/resources/application.properties

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@ spring.data.mongodb.uri=mongodb://localhost:27017/movies?locale=en
22
spring.data.mongodb.username=
33
spring.data.mongodb.password=
44

5-
#open API path
6-
springdoc.api-docs.path=/api-docs
5+
#Setting Open API path
6+
springdoc.api-docs.path=/api-docs
7+
8+
#Expose all actuator endpoints
9+
management.endpoints.web.exposure.include=*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
###
2+
GET http://127.0.0.1:8080/actuator/health
3+
4+
###
5+
GET http://127.0.0.1:8080/actuator
6+
7+
###
8+
GET http://127.0.0.1:8080/actuator/info
9+
10+
###
11+
GET http://127.0.0.1:8080/actuator/beans
12+
13+
###
14+

0 commit comments

Comments
 (0)