2

My problem is that i'm not able to create test which use in memory database. All the times got errors, for example:

Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages

How could i do the test with H2 inmemory database?

Entity class:

@Entity
@Table(name = "names")
public class Names{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "name_id")
    private int id;

    @Column(name = "name")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Repository class:

@Repository("namesRepository")
public interface NamesRepository extends JpaRepository<Names, Long> {

    Names findByName(String name);
    List<Names> findAll();
}

Database config class:

@Configuration
public class DatabaseConfig {

    @Bean
    @ConfigurationProperties(prefix="spring.dbProfileService")
    @Primary
    public DataSource createProfileServiceDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Autowired
    public JdbcTemplate createJdbcTemplate_ProfileService(DataSource profileServiceDS) {
        return new JdbcTemplate(profileServiceDS);
    }

}

application.yml

spring:
      application:
        name: app
      dbProfileService: 
        driverClassName: org.postgresql.Driver
        url: "jdbc:postgresql://localhost/postgres"
        password: "postgres"
        username: "postgres"
        testOnBorrow: false
        testWhileIdle: false
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 30000
        validationQuery: SELECT 1
        max-active: 15
        max-idle: 10
        max-wait: 8000

Test class:

@RunWith(SpringRunner.class)
  @DataJpaTest
  @SpringBootConfiguration
    public class NamesTest {

        @Autowired
        private NamesRepository names;

        @Test
        public void firsTest(){

        }
    }

Gradle dependencies:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.2.RELEASE'
    // https://mvnrepository.com/artifact/com.h2database/h2
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'

    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile(group: 'org.postgresql', name: 'postgresql', version: '42.2.2')
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-cassandra', version: '2.0.0.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.1.RELEASE'
    compile group: 'org.hibernate', name: 'hibernate-validator', version: '4.0.2.GA'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '2.0.1.RELEASE'
}
0

1 Answer 1

7

Please add @AutoConfigurationPackage to the @SpringBootConfiguration class, it needed because there is no @SpringBootApplication class in the sample:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigurationPackage
@SpringBootConfiguration
public class NamesTest {

    @Autowired
    private NamesRepository names;

    @Test
    public void firsTest(){

    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

After your changes: java.lang.IllegalStateException: Failed to load ApplicationContext ->>Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
Remove the dependency spring-boot-starter-test
disable JpaRepositoriesAutoConfiguration @EnableAutoConfiguration(exclude = { JpaRepositoriesAutoConfiguration.class })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.