java - 如何为具有spring存储库代码的@Component类编写集成测试?

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

我正在为以下类 @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();

    }

}

最佳答案

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

@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();

   }

关于java - 如何为具有spring存储库代码的@Component类编写集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54728704/

相关文章:

java - 从外部类处理 JFrame 组件的最佳方法

java - 没有平方根的归一化空间 vector

java - drl 文件中的 Drool 全局变量初始化

java - 来自 Spring MVC 中 LocalDateTime(java 8) 的 Json 字符串

java - Spring Controller 用于表单处理

java - SpringBoot org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'requestMappingHandlerMapping'

java - 我将如何组合 BiPredicate 和 Predicate?

Spring Boot 2 - 基于过滤器的 JWT Spring Security 实现中的 403 而不是 401

spring - 对多个测试类使用一个 @TestPropertySource

java - 如何从Spring Boot连接在线MongoDB数据库?