<commit_before>---
layout: docs
title: Testing
permalink:  testing/
prev: spring/validation
next: spring/tutorial
---

<div class="toc"></div>

The following test stack is included in the RESThub test module:

* Test framework with [TestNG {{site.testng-version}}](http://testng.org/doc/documentation-main.html).
  If you use Eclipse, don't forget to install the [TestNG plugin](http://testng.org/doc/eclipse.html).
* Assertion with [Fest Assert {{site.fest-assert-version}}](https://github.com/alexruiz/fest-assert-{{site.fest-assert-short-version}}.x/wiki)
* Mock with [Mockito {{site.mockito-version}}](http://code.google.com/p/mockito/)

RESThub also provides generic classes in order to make testing easier.

* `AbstractTest`: base class for your non transactional Spring aware unit tests
* `AbstractTransactionalTest`: base class for your transactional unit tests,
  preconfigured with Spring test framework
* `AbstractWebTest`: base class for your unit tests that need to run an embedded servlet container.

## Maven dependency

In order to use it in your project, add the following snippet to your pom.xml:

```xml
<dependency>
    <groupId>org.resthub</groupId>
    <artifactId>resthub-test</artifactId>
    <version>{{site.spring-stack-version}}</version>
    <scope>test</scope>
</dependency>
```

## Data provisioning and cleanup

It is recommended to initialize and cleanup test data shared by your tests using methods annotated with
TestNG's `@BeforeMethod` and `@AfterMethod` and using your repository or service classes.

<div class="alert alert-warning">
    With JPA the default deleteAll() method does not manage cascade delete, so for your data cleanup
    you should use the following code in order to get your entities removed with cascade delete support:
</div>

```java
Iterable<MyEntity> list = repository.findAll();
for (MyEntity entity : list) {
    repository.delete(entity);
}
```

## Usage

* `AbstractTest` or `AbstractTransactionalTest`:

```java
@ActiveProfiles("resthub-jpa")
public class SampleRepositoryTest extends AbstractTransactionalTest {

    private SampleRepository repository;

    @Inject
    public void setRepository(SampleRepository repository) {
        this.repository = repository;
    }

    @AfterMethod
    public void tearDown() {
        for (Sample resource : repository.findAll()) {
            repository.delete(resource);
        }
    }

    @Test
    public void testSave() {<commit_msg>Add missing bonecp profile in tests<commit_after>---
layout: docs
title: Testing
permalink:  testing/
prev: spring/validation
next: spring/tutorial
---

<div class="toc"></div>

The following test stack is included in the RESThub test module:

* Test framework with [TestNG {{site.testng-version}}](http://testng.org/doc/documentation-main.html).
  If you use Eclipse, don't forget to install the [TestNG plugin](http://testng.org/doc/eclipse.html).
* Assertion with [Fest Assert {{site.fest-assert-version}}](https://github.com/alexruiz/fest-assert-{{site.fest-assert-short-version}}.x/wiki)
* Mock with [Mockito {{site.mockito-version}}](http://code.google.com/p/mockito/)

RESThub also provides generic classes in order to make testing easier.

* `AbstractTest`: base class for your non transactional Spring aware unit tests
* `AbstractTransactionalTest`: base class for your transactional unit tests,
  preconfigured with Spring test framework
* `AbstractWebTest`: base class for your unit tests that need to run an embedded servlet container.

## Maven dependency

In order to use it in your project, add the following snippet to your pom.xml:

```xml
<dependency>
    <groupId>org.resthub</groupId>
    <artifactId>resthub-test</artifactId>
    <version>{{site.spring-stack-version}}</version>
    <scope>test</scope>
</dependency>
```

## Data provisioning and cleanup

It is recommended to initialize and cleanup test data shared by your tests using methods annotated with
TestNG's `@BeforeMethod` and `@AfterMethod` and using your repository or service classes.

<div class="alert alert-warning">
    With JPA the default deleteAll() method does not manage cascade delete, so for your data cleanup
    you should use the following code in order to get your entities removed with cascade delete support:
</div>

```java
Iterable<MyEntity> list = repository.findAll();
for (MyEntity entity : list) {
    repository.delete(entity);
}
```

## Usage

* `AbstractTest` or `AbstractTransactionalTest`:

```java
@ActiveProfiles({"resthub-jpa", "resthub-pool-hikaricp"})
public class SampleRepositoryTest extends AbstractTransactionalTest {

    private SampleRepository repository;

    @Inject
    public void setRepository(SampleRepository repository) {
        this.repository = repository;
    }

    @AfterMethod
    public void tearDown() {
        for (Sample resource : repository.findAll()) {
            repository.delete(resource);
        }
    }

    @Test
    public void testSave() {
{'fraction_non_alphanumeric': 0.10729440290967872, 'fraction_numerical': 0.0, 'mean_word_length': 4.339805825242719, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2060918', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1337, 'n_words': 476}