Spring Boot存储库在测试期间无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot存储库在测试期间无法正常工作相关的知识,希望对你有一定的参考价值。

我搜索了谷歌和SO的答案,并尝试了许多变化,没有运气。我正在尝试过去几天为我的Spring Boot端点启用集成测试。以下是详细信息:

错误是在EnclosureController类中找到的NullPointerException(我在注释中用NULL标记了对象)

如果存在一种更有效的方式来执行集成测试,与MockMvc相比,我对建议非常开放。

TestClass(在root.package.test中)

@RunWith(SpringRunner.class)
@WebMvcTest(EnclosureController.class)
public class EnclosureControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private EnclosureRepository enclosureRepository;

//static final strings for Enclosure initialization

@Test
public void createEnclosureAPI() throws Exception
{
    mvc.perform( MockMvcRequestBuilders
            .post("/enclosure")
            .header("Authorization", "TEST")
            .content(asJsonString(new Enclosure(ENCLOSURE_TITLE, ENCLOSURE_LOCATION, DIMENSIONAL_UNITS, ENCLOSURE_LENGTH, ENCLOSURE_WIDTH, ENCLOSURE_HEIGHT)))
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isCreated())
            .andDo(print())
            .andExpect(MockMvcResultMatchers.jsonPath("$.enclosureId").exists());
}
}

EnclosureController(我删除了身份验证检查,因为错误与repo有关)

@RestController
public class EnclosureController {

final
private EnclosureRepository repository;

@Autowired
public EnclosureController(EnclosureRepository repository) {
    this.repository = repository;
}

@RequestMapping(value = {"/enclosure"},
        method = RequestMethod.POST,
        consumes = "application/json",
        produces = APPLICATION_JSON_VALUE)
@ResponseBody
@Async("asyncExecutor")
public CompletableFuture<Enclosure> createEnclosure(
        @Valid @RequestBody Enclosure request,
        @RequestHeader(value = "Authorization") String auth,
        HttpServletResponse response
) {

    //NULL on repository (Optional is never returned. NullPointerExcep thrown on repository.save)
    int enclosureId = Optional.of(repository.save(request)).orElse(new Enclosure(0)).getEnclosureId();

    if (enclosureId > 0)
        response.setStatus(HttpServletResponse.SC_CREATED);

    return CompletableFuture.completedFuture(repository.findByEnclosureId(enclosureId));

}

}
@RequestMapping(value = {"/enclosure/{id}"},
        method = RequestMethod.GET)
@ResponseBody
@Async("asyncExecutor")
public CompletableFuture<Enclosure> getSingleEnclosure(
        @PathVariable("id") int id,
        @RequestHeader(value = "Authorization") String auth,
        HttpServletResponse response
) {

    return  CompletableFuture.completedFuture(repository.findByEnclosureId(id));

}

知识库

@Repository
public interface EnclosureRepository extends CrudRepository<Enclosure, Integer> {
Enclosure findByEnclosureId(Integer enclosureId);
List<Enclosure> findAll();
}

RepositoryImpl(用于bean声明。注意删除此帖子不需要的方法)

public class EnclosureRepositoryImpl implements EnclosureRepository {

private static ConcurrentHashMap<Integer, Optional<Enclosure>> repo = new ConcurrentHashMap<>();
private static AtomicInteger maxId = new AtomicInteger();

@Override
public Enclosure findByEnclosureId(Integer enclosureId) {
    return repo.get(enclosureId).orElse(new Enclosure());
}

@Override
public Enclosure save(Enclosure entity) {

    repo.put(maxId.incrementAndGet(), Optional.of(entity));

    return repo.get(maxId).orElse(new Enclosure());
}

@Override
public Optional<Enclosure> findById(Integer integer) {
    return repo.get(integer);
}

@Override
public boolean existsById(Integer integer) {
    return repo.containsKey(integer);
}
}

应用

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

组态

@Configuration
@EnableJpaRepositories(basePackages = {
    "root.package.model.repository"
})
@EnableTransactionManagement
@EnableAsync
public class BeanConfig {

@Override
@Bean(name = "asyncExecutor")
public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(3);
    executor.setMaxPoolSize(100);
    executor.setQueueCapacity(100);
    executor.setThreadNamePrefix("AGMSpringAsyncThread-");
    executor.initialize();

    return executor;
}

@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
}

@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
                                                            Environment env) {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource);
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    entityManagerFactoryBean.setPackagesToScan("io.colby.model.repository");

    Properties jpaProperties = new Properties();

    //Configures the used database dialect. This allows Hibernate to create SQL
    //that is optimized for the used database.
    jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));

    //Specifies the action that is invoked to the database when the Hibernate
    //SessionFactory is created or closed.
    jpaProperties.put("hibernate.hbm2ddl.auto",
            env.getRequiredProperty("hibernate.hbm2ddl.auto")
    );

    //Configures the naming strategy that is used when Hibernate creates
    //new database objects and schema elements
    jpaProperties.put("hibernate.ejb.naming_strategy",
            env.getRequiredProperty("hibernate.ejb.naming_strategy")
    );

    //If the value of this property is true, Hibernate writes all SQL
    //statements to the console.
    jpaProperties.put("hibernate.show_sql",
            env.getRequiredProperty("hibernate.show_sql")
    );

    //If the value of this property is true, Hibernate will format the SQL
    //that is written to the console.
    jpaProperties.put("hibernate.format_sql",
            env.getRequiredProperty("hibernate.format_sql")
    );

    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
}

@Bean(destroyMethod = "close")
DataSource dataSource(Environment env) {
    HikariConfig dataSourceConfig = new HikariConfig();
    dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
    dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
    dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
    dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));

    return new HikariDataSource(dataSourceConfig);
}

@Bean
public EnclosureRepository enclosureRepository(){
    return new EnclosureRepositoryImpl();
}
}

附件

@Component
@Entity
@Table(name="enclosure")
public class Enclosure {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "enclosure_id", nullable = false, updatable = false)
@JsonProperty("enclosure-id")
private Integer enclosureId;

@Column(name = "title")
@Size(max = 255)
@NotBlank
private String title;

@Column(name = "location")
@Size(max = 255)
private String location;

@Column(length = 25, name = "dimension_units")
@Size(max = 25)
@JsonProperty("dimension-units")
private String dimensionUnits;

@CreationTimestamp
@Column(nullable = false, name = "insert_timestamp")
@JsonProperty("created-date-time")
private LocalDateTime insertTimestamp;

@UpdateTimestamp
@Column(name = "update_timestamp")
@JsonProperty("last-updated-date-time")
private LocalDateTime updateTimestamp;


@Column(length = 5, precision = 2)
private double length;
@Column(length = 5, precision = 2)
private double width;
@Column(length = 5, precision = 2)
private double height;

public Enclosure(String title,
                 String location,
                 String dimensionUnits,
                 double length, double width, double height) {
    this.title = title;
    this.location = location;
    this.dimensionUnits = dimensionUnits;
    this.length = length;
    this.width = width;
    this.height = height;
}

public Enclosure(int enclosureId){
    this.enclosureId = enclosureId;
}

public Enclosure(){

}
//Getters and setters...

目录树

.
└── main
    ├── java
    │   └── root
    │       └── package
    │           ├── Application.java
    │           ├── configuration
    │           │   ├── BeanConfig.java
    │           ├── model
    │           │   ├── entity
    │           │   │   ├── Enclosure.java
    │           │   └── repository
    │           │       ├── EnclosureRepository.java
    │           │       ├── EnclosureRepositoryImpl.java
    │           ├── routes
    │           │   ├── enclosure
    │           │   │   └── controller
    │           │   │       └── EnclosureController.java
    │           └── test
    │               └── routes
    │                   └── enclosure
    │                       └── EnclosureControllerTest.java
    ├── resources
    │   ├── application.properties
    └── test
        └── java

application.properties

 #Database Configuration
    db.driver=org.h2.Driver
    db.url=jdbc:h2:mem:datajpa
    db.username=sa
    db.password=

    spring.jackson.default-property-inclusion=non_null

    # Details for our datasource
    spring.datasource.url = jdbc:postgresql://host/db
    spring.datasource.username = user
    spring.datasource.password = pass

    # Hibernate properties
    spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect
    spring.jpa.show-sql = true
    spring.jpa.hibernate.ddl-auto = create-drop
    spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
    spring.jpa.properties.hibernate.format_sql=true

注意我在test.java中有测试类,但是我想让测试在root.package子目录中运行,然后使用@ComponentScan指向要扫描的包树。

我正在查看以下教程以尝试让测试工作:

http://www.springboottutorial.com/integration-testing-for-spring-boot-rest-services
https://www.baeldung.com/spring-boot-testing
答案

通过注释创建模拟时,即@Mock,您需要初始化它们。最好的方法是调用:

MockitoAnnotations.initMocks(this);

在使用@Before注释的方法中,以便在调用测试之前创建模拟。

以上是关于Spring Boot存储库在测试期间无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

使用 gradle 时,在 Spring Boot 测试期间无法加载驱动程序类:“org.h2.Driver”

Spring Cloud FeignClients 在应用程序启动期间无法正常工作

Spring Boot + Spring Data JPA + 事务无法正常工作

在 Groovy 测试期间 Spring Autowire bean NullPointerException 但在运行时工作正常

Spring Security,Boot:安全规则无法正常工作

Spring Boot 搜索无法正常工作