Skip to content

PetControllerTests.java⚓︎

Overview⚓︎

The PetControllerTests.java file contains the test cases for the PetController class in the org.springframework.samples.petclinic.owner package. This file is part of the Pet Clinic project and is used to test the functionality of the PetController class.

Table of Contents⚓︎

  1. Prerequisites
  2. Usage
  3. Methods
  4. Useful details

Prerequisites⚓︎

There are no specific prerequisites mentioned in the code file.

Usage⚓︎

To use the PetControllerTests.java file in a project, follow these steps: 1. Make sure the project has the required dependencies and frameworks mentioned in the code file. 2. Create an instance of the PetControllerTests class. 3. Run the test methods to test the functionality of the PetController class.

Methods⚓︎

The PetControllerTests.java file contains the following methods:

  1. setup(): This method is annotated with @BeforeEach and is executed before each test case. It sets up the mock objects and defines the behavior of the mock objects using the given() method from Mockito.

  2. testInitCreationForm(): This method is annotated with @Test and tests the initCreationForm() method of the PetController class. It performs a GET request to the "/owners/{ownerId}/pets/new" endpoint and asserts the status, view name, and model attributes.

  3. testProcessCreationFormSuccess(): This method is annotated with @Test and tests the processCreationFormSuccess() method of the PetController class. It performs a POST request to the "/owners/{ownerId}/pets/new" endpoint with the required parameters and asserts the status and view name.

  4. testProcessCreationFormHasErrors(): This method is annotated with @Test and tests the processCreationFormHasErrors() method of the PetController class. It performs a POST request to the "/owners/{ownerId}/pets/new" endpoint with missing required parameters and asserts the model attributes, status, and view name.

  5. testInitUpdateForm(): This method is annotated with @Test and tests the initUpdateForm() method of the PetController class. It performs a GET request to the "/owners/{ownerId}/pets/{petId}/edit" endpoint and asserts the status, model attributes, and view name.

  6. testProcessUpdateFormSuccess(): This method is annotated with @Test and tests the processUpdateFormSuccess() method of the PetController class. It performs a POST request to the "/owners/{ownerId}/pets/{petId}/edit" endpoint with the required parameters and asserts the status and view name.

  7. testProcessUpdateFormHasErrors(): This method is annotated with @Test and tests the processUpdateFormHasErrors() method of the PetController class. It performs a POST request to the "/owners/{ownerId}/pets/{petId}/edit" endpoint with missing required parameters and asserts the model attributes, status, and view name.

Useful details⚓︎

The PetControllerTests.java file uses the following frameworks and dependencies:

  • org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest: This annotation is used to enable the Spring MVC testing support for the PetController class.

  • org.springframework.boot.test.mock.mockito.MockBean: This annotation is used to create a mock bean of the OwnerRepository class.

  • org.springframework.test.web.servlet.MockMvc: This class is used for testing Spring MVC controllers by providing a fake implementation of the web application.

  • org.mockito.BDDMockito.given(): This method is used to define the behavior of the mock objects.

  • org.springframework.samples.petclinic.owner.PetController: This is the class under test.

  • org.springframework.samples.petclinic.owner.PetTypeFormatter: This class is used as a filter for component scanning.

The code is licensed under the Apache License, Version 2.0.