Spring - Spring 5.2.0.RELEASE 中的单元测试存储库

问题描述 投票:0回答:1

我有这门课:

@Configuration
@EnableJpaRepositories(basePackages = "es.calero.repositories")
public class TestConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
        dataSource.setUsername("sa");
        dataSource.setPassword("");
        return dataSource;
    }



    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan("es.calero.model.inscriptions");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }
}

还有这个:

@ContextConfiguration(classes = es.calero.repositories.utils.TestConfig.class)
@Transactional
public class AutorisationSortieRepositoryTest {

    @Autowired
    private AutorisationSortieRepository autorisationSortieRepository;

    private es.calero.model.autorisation.AutorisationSortie autorisationSortie;

    @Before
    public void setUp() {

        // Insert test data into the in-memory database
        autorisationSortie = new AutorisationSortie();
        autorisationSortie.setEnfantId(1L);
        autorisationSortie.setDtDebut(new Date());
        autorisationSortie.setDtFin(new Date(System.currentTimeMillis() + 86400000L)); // +1 day

        autorisationSortieRepository.save(autorisationSortie);
    }

    @Test
    public void testFindActiveAutorisationSortieByEnfantId() {
        // Create a reference date within the range
        Date dtRef = new Date(System.currentTimeMillis() + 43200000L); // +12 hours

        List<AutorisationSortie> result = autorisationSortieRepository.findActiveAutorisationSortieByEnfantId(1L, dtRef);

        // Assert the result is not empty and contains the expected AutorisationSortie
        Assert.assertTrue("Result should not be empty", !result.isEmpty());
        assertEquals("Result should contain the test AutorisationSortie", autorisationSortie, result.get(0));
    }

    @Test
    public void testFindActiveAutorisationSortieByEnfantId_NoResult() {
        // Create a reference date outside the range
        Date dtRef = new Date(System.currentTimeMillis() - 86400000L); // -1 day

        List<AutorisationSortie> result = autorisationSortieRepository.findActiveAutorisationSortieByEnfantId(1L, dtRef);

        // Assert that no results are found
        Assert.assertTrue("Result should be empty", result.isEmpty());
    }

}

但是我有一个空指针

autorisationSortieRepository.save(autorisationSortie);:

enter image description here

添加

@RunWith(SpringJUnit4ClassRunner.class)
时出现此错误:

   initializationError(es.calero.repositories.autorisation.AutorisationSortieRepositoryTest)  Time elapsed: 0.011 sec  <<< ERROR!
java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:155)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NoClassDefFoundError: org/springframework/util/ConcurrentLruCache
    at org.springframework.test.context.TestContextAnnotationUtils.<clinit>(TestContextAnnotationUtils.java:74)
    at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:165)
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:138)
    ... 25 more
Caused by: java.lang.ClassNotFoundException: org.springframework.util.ConcurrentLruCache
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 28 more
Results :
Tests in error: 
  initializationError(es.calero.repositories.autorisation.AutorisationSortieRepositoryTest): Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
java spring junit spring-data-jpa eclipselink
1个回答
0
投票

首先,你必须在

@RunWith(SpringJUnit4ClassRunner.class)
类上添加
AutorisationSortieRepositoryTest
才能使bean的注入起作用。

其次,请使用

spring-data-jpa
版本
2.2.0.RELEASE
,它使用Spring依赖项的
5.2.0.RELEASE

更新

pom.xml
中的 spring-data-jpa 依赖项:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

看看这是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.