mardi 11 septembre 2018

Failed to load ApplicationContext while trying to test database

I'm writing an application that uses a database. However, when I try to run the test I get an error IllegalState Failed to load ApplicationContext. For test I created application.properties file , which is located in src/test/resources:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

I have a Book.class:

@Entity
@Table(name = "book")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable = false)
    private String title;
    private String description;
    @Column(name = "release_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date releaseDate;
    @Column(name = "cover_image")
    @OneToOne(cascade = CascadeType.MERGE)
    private UploadFile coverImage;
    @OneToOne(cascade = CascadeType.MERGE)
    private UploadFile content;
    @ManyToMany
    @JoinTable(name = "book_category", joinColumns = {@JoinColumn(name = "book_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "category_id", referencedColumnName = "id")})
    private Set<Category> categories;

    public Book() { }

    public Book(String title){
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public Long getId() {
        return id;
    }

    public UploadFile getCoverImage() {
        return coverImage;
    }

    public void setCoverImage(UploadFile coverImage) {
        this.coverImage = coverImage;
    }

    public UploadFile getContent() {
        return content;
    }

    public void setContent(UploadFile content) {
        this.content = content;
    }

    public Set<Category> getCategories() {
        return categories;
    }

    public void setCategories(Set<Category> categories) {
        this.categories = categories;
    }
}

And an interface that extends JpaRepository:

public interface BookRepository extends JpaRepository<Book, Long> {
}

Test class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
public class DatabaseTest {
    @Autowired
    private BookRepository bookRepository;

    @Test
    public void testingDatabase(){
        Book book = bookRepository.save(new Book("Przemineło z wiatrem"));
        Book foundBook = bookRepository.findById(book.getId()).orElse(new Book());

        assertEquals(book.getId(), foundBook.getId());
    }
}

I tried to change the annotation in DatabaseClass from @Autowired to @Resource however, it didn't help. Maybe someone knows how to solve this problem?

Aucun commentaire:

Enregistrer un commentaire