javajpajunitundefinedassertthat

The method assertThat(List<Flight>) is undefined for the type SpringDataOverviewApplicationTestsJava(67108964)


I am trying to follow along with a pluralsight demo course on JPA and the test I just built is throwing an error on the assertThat method and I can't figure out why. I'm sure this is super obvious, but I can't find the solution on here or on the java documentation examples. Please send help in this new year.

The error message

The method assertThat(List<Flight>) is undefined for the type SpringDataOverviewApplicationTestsJava(67108964)

The code

package com.pluralsight.springdataoverview;

import java.time.LocalDateTime;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import com.pluralsight.springdataoverview.entity.Flight;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;


@DataJpaTest
class SpringDataOverviewApplicationTests {

    @Autowired
    private EntityManager entityManager;

    @Test
    public void verifyFlightCanBeSaved() {
        final Flight flight = new Flight();
        flight.setOrigin("Amsterdam"); 
        flight.setDestination("New york");
        flight.setScheduledAt(LocalDateTime.parse("2011-12-13T12:12:00"));

        entityManager.persist(flight);

        final TypedQuery<Flight> results = entityManager
            .createQuery("SELECT f FROM Flight f", Flight.class);

            final List<Flight> resultList = results.getResultList();

            assertThat(resultList)
                .hasSize(1)
                .first()
                .isEqualTo(flight);
    }

}

Solution

  • Be sure to statically import the used assertion method:

    import static org.assertj.core.api.Assertions.assertThat;
    

    Note that the assertj dependency also needs to be present, so declare it e.g. via gradle: testImplementation("org.assertj:assertj-core:3.22.0")

    https://assertj.github.io/doc/