Репозиторий Spring Boot не работает во время тестирования - PullRequest
0 голосов
/ 10 марта 2019

Я искал ответы в Google и SO и перепробовал множество вариантов без удачи.Я пытаюсь включить интеграционное тестирование для моих конечных точек Spring Boot в течение последних нескольких дней.Вот подробности:

Ошибка - это исключение NullPointerException, найденное в классе EnclosureController (где я отметил объект в комментарии с помощью 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 (я удалил проверки аутентификации, так как ошибкаотносится к репо)

@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 указал на дерево пакетов для сканирования.

Я смотрел на следующееtutoriТакже можно попробовать запустить тестирование:

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

1 Ответ

1 голос
/ 10 марта 2019

При создании макетов с помощью аннотаций, т. Е. @Mock, их необходимо инициализировать. Лучший способ сделать это, вызвав:

MockitoAnnotations.initMocks(this);

внутри метода, помеченного @Before, так что макеты создаются прямо перед вызовом тестов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...