Search code examples
javaangularspringtestingweb-api-testing

In the context of a common Spring and Angular application, what is the API?


There is a vast amount of information available on API's. Guru99 has a good page on it. The information available is pretty general though. I am not sure exactly how it applies to the work I am familiar with. My work being that I commonly develop web applications with Spring and Angular.

In this context what is the API? Is it the user interface where the user enters text and clicks buttons? Is it the Spring DispatcherServlet which handles incoming requests from the front-end and directs them to the correct back-end Spring Controller method? Is it the Spring Controller itself? Is the API in the front-end service classes which send requests back to the back-end? These service classes would contain methods using rxjs Observables like the one below.

Angular

getCards(): Observable<Card[]> {
  return this.http.get<Card[]>(this.baseUrl+'cards')
  .pipe(catchError(this.handleError<Card[]>('getCards', [])));
}

While a back-end controller method would look like this:

Spring

@RestController
@RequestMapping("cards")
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class CardControl {
    private final CardService cardService;

    @GetMapping
    public ResponseEntity<List<Card>> getCards(){
        List<Card> cards = cardService.findAll();
        return new ResponseEntity<>(cards, HttpStatus.OK);
    }
}

If I was to do API testing on such an application what exactly would I be testing? Would this testing occur on the back-end, the front-end, or both?


Solution

  • Generally, when we talk about APIs (in the context of web applications) we generally talk about the back-end. So in your case, it would be your spring controller:

    @RestController
    @RequestMapping("cards")
    @AllArgsConstructor(onConstructor = @__(@Autowired))
    public class CardControl {
        private final CardService cardService;
    
        @GetMapping
        public ResponseEntity<List<Card>> getCards(){
            List<Card> cards = cardService.findAll();
            return new ResponseEntity<>(cards, HttpStatus.OK);
        }
    }
    

    So if you were to do API testing, you would be sending a GET [URL]/cards request to your back-end and making sure it returns what you expect.

    There a couple of tools you could use for API testing in your context.

    • Postman is one with a nice UI where you can define requests and tests quite easily.
    • Rest assured is a Java library that allows you send and test requests in Java.