【问题标题】:How to write Integration Test for @Component class which have spring repository code?如何为具有spring存储库代码的@Component类编写集成测试?
【发布时间】:2019-07-10 17:46:38
【问题描述】:

我正在为以下类@Component 类编写 Junit 测试用例。我实际上将 Sql Server 用于我的原始应用程序。但是,作为测试的一部分,我在内存 h2 db 中使用。我想在没有的情况下测试这个类启动原始应用程序。如果我在测试类中使用存储库,我可以看到测试中返回的数据,但正是当我调用 @Component 类中的 void 方法获取 NULL 指针时。 这是我的代码。

    @Component
    public class CandidatesTableServiceImpl {



        @Autowired
        private CandidatesTableRepository candidatesTableRepository;

        @Transactional
        public void getAllRecordsFromCandidateTable() throws ParseException {

            List<CandidatesTable> customerRecord = candidatesTableRepository
                    .findByStatus(status);

/** This method throwing Null pointer exception when calling from method **/


    }

我的 src/test/reources/application.properties

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
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
# Create DDL
spring.jpa.hibernate.ddl-auto=create

我的测试类看起来像这样

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
@Transactional
public class CandidateTableTest {

    @Autowired
    TestEntityManager entityManager;

    @Autowired
    CandidatesTableRepository candidatesTableRepository;

    String status;

    @Before
    public void init() {
        CandidatesTableServiceImpl candidatesTableServiceImpl= new candidatesTableServiceImpl()

    }

    @Test
    public void checkSaveMethod() throws ParseException {

        CandidatesTable candidatesTable = new CandidatesTable();

        candidatesTable.setStatus(status);
        candidatesTable.setCandidatesTableID(1);
        candidatesTable.setAccountNumber("2000321654");

        candidatesTableRepository.save(candidatesTable);
        this.entityManager.persist(candidatesTable);


/** This method works fine **/
                  List<CandidatesTable> alertRecord = candidatesTableRepository.findByStatus(status);


/** This method throwing NUll Pointer excption **/
        candidatesTableServiceImpl.getAllRecordsFromCandidateTable();

    }

}

【问题讨论】:

    标签: java spring spring-boot junit spring-data-jpa


    【解决方案1】:

    您没有指定扫描组件的位置,请使用

    @ComponentScan(value = ["your.package.name"])
    

    你还需要刷新数据

       @Test
       public void checkSaveMethod() throws ParseException {
    
           CandidatesTable candidatesTable = new CandidatesTable();
    
           candidatesTable.setStatus(status);
           candidatesTable.setCandidatesTableID(1);
           candidatesTable.setAccountNumber("2000321654");
    
           candidatesTableRepository.save(candidatesTable);
           this.entityManager.persist(candidatesTable);
           this.entityManager.flush();
    
    /** This method works fine **/
                     List<CandidatesTable> alertRecord = candidatesTableRepository.findByStatus(status);
    
    
    /** This method throwing NUll Pointer excption **/
           candidatesTableServiceImpl.getAllRecordsFromCandidateTable();
    
       }
    
    

    【讨论】:

    • 我尝试使用 @ComponentScan 得到同样的问题。
    猜你喜欢
    • 2021-07-28
    • 2019-02-12
    • 2018-11-24
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多