尝试使用Spring Data执行集成测试JPA Repositories返回NullPointerException

时间:2016-04-27 10:45:11

标签: spring testing repository integration spring-data-jpa

我正在尝试为Spring 4 MVC项目编写集成测试。在项目中,我使用的是Spring Data JPA Repositories和Hibernate。

下面是两个文件,Integration Test本身和用于设置我的持久性配置的配置。

PersistenceTestConfiguration.java

package <project>.cms.rest.test.configuration;

import <project>.cms.rest.entity.EntityMarker;
import <project>.cms.rest.repository.RepositoryMarker;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.hibernate3.encryptor.HibernatePBEEncryptorRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses = {RepositoryMarker.class})
public class PersistenceTestConfiguration {

    @Bean
    public EntityManagerFactory entityManagerFactory() throws NamingException {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        vendorAdapter.setShowSql(true);
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setPackagesToScan(EntityMarker.class.getPackage().getName());
        factoryBean.setDataSource(dataSource());
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws NamingException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory());
        return transactionManager;
    }

    @Bean
    public HibernateExceptionTranslator exceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

    @Bean
    public EmbeddedDatabase dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.H2).build();
    }
}

我遇到的问题是虽然我的PersistenceTestConfiguration类应该实例化相应的存储库 - 但它们将作为null进入服务类。

如果我手动@Mock测试类中的存储库,那么它们在服务中不再为null,但是总是返回null,例如调用.save(entity)方法。

我怀疑我做的事情显然很愚蠢,但似乎无法弄明白。

任何指向正确的方向都会受到赞赏。

编辑1:

最简单的可能测试

package <project>.cms.rest.test.service;

import <project>.cms.rest.resource.PostalAddressResource;
import <project>.cms.rest.service.PostalAddressService;
import <project>.cms.rest.service.impl.PostalAddressServiceImpl;
import <project>.cms.rest.test.configuration.PersistenceTestConfiguration;
import liquibase.Liquibase;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.inject.Inject;
import java.sql.Connection;

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { PersistenceTestConfiguration.class })
@TestExecutionListeners(mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class PostalAddressServiceIntegrationTest {

    @InjectMocks
    private PostalAddressService postalAddressService = new PostalAddressServiceImpl();

    @Inject
    private EmbeddedDatabase database;

    @Before
    public void setupDatabase() throws java.sql.SQLException, LiquibaseException {
        MockitoAnnotations.initMocks(this);

        Liquibase liquibase = new Liquibase(
                "src/liquibase/liquibase-master.xml",
                new FileSystemResourceAccessor(),
                new JdbcConnection(database.getConnection())
        );

        liquibase.update("");
    }

    @After
    public void tearDown() throws java.sql.SQLException, LiquibaseException {
        Connection connection = database.getConnection();
        JdbcConnection liquibaseConnection = new JdbcConnection(connection);

        Liquibase liquibase = new Liquibase(
                "src/liquibase/liquibase-master.xml",
                new FileSystemResourceAccessor(),
                liquibaseConnection
        );

        liquibase.dropAll();
    }

    @Test
    public void testAddNewPostalAddress() {
        PostalAddressResource postalAddressResource = new PostalAddressResource();

        postalAddressResource.setBuildingNameOrNumber("1");
        postalAddressResource.setFirstLine("Test Lane");
        postalAddressResource.setSecondLine("Testville");
        postalAddressResource.setCountry("Testshire");
        postalAddressResource.setPostCode("TE5 7ER");
        postalAddressResource.setCountry("United Kingdom");

        PostalAddressResource result = postalAddressService.createPostalAddress(postalAddressResource);

        Assert.notNull(result);
    }
}

产生的堆栈跟踪

java.lang.NullPointerException
    at <project>.cms.rest.service.impl.PostalAddressServiceImpl.createPostalAddress(PostalAddressServiceImpl.java:66)
    at <project>.cms.rest.test.service.PostalAddressServiceIntegrationTest.testAddNewPostalAddress(PostalAddressServiceIntegrationTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

PostalAddressServiceImpl的第66行如下:

} while(repository.countByPublicId(publicId) > 0);

编辑2:

创建一个直接使用Repository而不是通过Service工作的测试,具有:

@Inject
private PostalAddressRepository repository;
顶部的

表现自己。

所以似乎@InjectMocks应该在Service类的依赖关系中连接存在问题,导致存储库为null。

2 个答案:

答案 0 :(得分:2)

看看我必须运行类似的东西,我也有一个像这样定义的加载器

@ContextConfiguration(loader = AnnotationConfigContextLoader.class,

和一些测试听众

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})

Spring中的集成测试有点乱,但在Spring Boot 1.4中已经整理了很多:

https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

答案 1 :(得分:1)

这已按照以下方式解决:

https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/

更改我的服务以使构造函数接受依赖项,而不是希望@InjectMocks将完成它本应该做的工作。