Olá pessoal! Meu código apresentou diversos erros ao criar as tabelas com o h2, um deles foi este:
2020-10-07 17:58:52.941 INFO 9140 --- [ main] b.com.alura.forum.ForumApplicationTests : Starting ForumApplicationTests on DESKTOP-0T3QLIL with PID 9140 (started by rapha in C:\Users\rapha\eclipse-workspace\_ALURA_CURSO_SPRINGBOOT\forum - Módulo 12)
2020-10-07 17:58:52.942 INFO 9140 --- [ main] b.com.alura.forum.ForumApplicationTests : The following profiles are active: test
2020-10-07 17:58:55.364 INFO 9140 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-10-07 17:58:55.488 INFO 9140 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 111ms. Found 3 JPA repository interfaces.
2020-10-07 17:58:56.432 INFO 9140 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-07 17:58:56.529 INFO 9140 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-07 17:58:56.642 INFO 9140 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-10-07 17:58:56.880 INFO 9140 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-07 17:58:57.083 WARN 9140 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-10-07 17:58:57.104 INFO 9140 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-10-07 17:58:57.388 INFO 9140 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-10-07 17:58:57.418 INFO 9140 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2020-10-07 17:58:58.331 INFO 9140 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService
Hibernate:
create table curso (
id bigint not null auto_increment,
categoria varchar(255),
nome varchar(255),
primary key (id)
) engine=InnoDB
2020-10-07 17:58:58.560 WARN 9140 --- [ task-1] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "
create table curso (
id bigint not null auto_increment,
categoria varchar(255),
nome varchar(255),
primary key (id)
) engine=InnoDB" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
Arquivo application-test.properties
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:alura-forum-test
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.initialization-mode=never
Classe CursoRepositoryTest
package br.com.alura.forum.repository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import br.com.alura.forum.model.Curso;
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
public class CursoRepositoryTest {
@Autowired
private CursoRepository repository;
@Autowired
private TestEntityManager tem;
@Test
public void buscarERetornaUmCursoPeloNome() {
String nomeCurso = "JAVA";
Curso c = new Curso();
c.setCategoria("Programação");
c.setNome(nomeCurso);
this.tem.persist(c);
Curso curso = this.repository.findByNome(nomeCurso);
Assert.assertNotNull(curso);
Assert.assertEquals(nomeCurso, curso.getNome());
}}