View on GitHub

reads

https://yazansneneh.github.io/reads/

Read: 13 - Related Resources and Integration Testing

class Address

 @OneToOne(mappedBy = "address")
  private Library library;
  1. The Repositories:: now I will create repository for each class and extended it from CrudRepository so they are exposed as resources

One-to-many Relationship

@ManyToOne
    @JoinColumn(name="library_id")
    private Library library;
@OneToMany(mappedBy = "library")
    private List<Book> books;
  1. The Repository
    • public interface BookRepository extends CrudRepository<Book, Long> { }
    • the book resource entity also exposed to make the relation of many books inside our library in the db.

Many-to-Many Relationship

  1. data model
    • A many-to-many relationship is extablished with @ManyToMany annotation.
    • example :
@ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "book_author", 
      joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), 
      inverseJoinColumns = @JoinColumn(name = "author_id", 
      referencedColumnName = "id"))
    private List<Book> books;
@ManyToMany(mappedBy = "books")
    private List<Author> authors;
  1. The Repository
    • public interface AuthorRepository extends CrudRepository<Author, Long> { }
    • expose the authors resource to establish the relationship

      Integration Testing in Spring

    • we use it to test the controllers behaviour
  2. Preparation
    • required dependencies junit-jupiter-engine, junit-jupiter-api, and Spring test.
    • for asserting results : Hamcrest and JSON path
  3. Spring MVC Test Configuration
    1. Enable Spring in Tests with JUnit 5.
      1. JUnit defines an extension interface through which classes can integrate with the JUnit test.
      2. adding the @ExtendWith annotation to test classes and specifying the extension class to load. To run the Spring test, use SpringExtension.class.
      3. @ContextConfiguration provide the ApplicationConfig.class config class, which loads the configuration needed for test test.
      4. @WebAppConfiguration, will load the web application context, it search from root and to override it i provide value=""
      5. example :
        @ExtendWith(SpringExtension.class)
        @ContextConfiguration(classes = { ApplicationConfig.class })
        @WebAppConfiguration
        public class GreetControllerIntegrationTest {
         ....
        }
        

        resource.

  4. The WebApplicationContext Object
    1. provides a web application configuration, it load app beans and controllers into context.
  5. Mocking Web Context Beans
    1. encapsulate app beans and make them available for testing.
    2. usage:
    3. Note : initialize the mockMvc object in the @BeforeEach annotated method so I don’t have to initialize it inside every test.
private MockMvc mockMvc;
@BeforeEach
public void setup() throws Exception {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}

resource

  1. Verify Test Configuration
@Test
public void givenWac_whenServletContext_thenItProvidesGreetController() {
    ServletContext servletContext = webApplicationContext.getServletContext();
    
    Assert.assertNotNull(servletContext);
    Assert.assertTrue(servletContext instanceof MockServletContext);
    Assert.assertNotNull(webApplicationContext.getBean("greetController"));
}

resource.

Writing Integration Tests

@Test
public void givenHomePageURI_whenMockMVC_thenReturnsIndexJSPViewName() {
    this.mockMvc.perform(get("/homePage")).andDo(print())
      .andExpect(view().name("index"));
}

MockMvc Limitations