title: Groovy - integration tests
body: <p>I write integration tests for my application using Spock and Spring Framework.</p>
  
  <p>I want to save some objects before each test and remove all after each test. </p>
  
  <p>But problem is that the id generated by Hibernate is not removed after each test. And when I create 2 objects before first test Hibernate generate id 1 and 2 and when I run test findById for id 1 and 2, test is success. But next test for id 1 and 2 is not success, because the objects in database have id 3 and 4.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>@ContextConfiguration
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  class ControllerSpec extends Specification {
  
      @Autowired
      private TestRestTemplate restTemplate
  
      @Autowired
      private MyRepository repository
  
      private EntityDTO dto
  
      private MyEntity entity1
      private MyEntity entity2
  
      @Before
      void set() {
          this.entity1 = new MyEntity(1L)
          this.entity2 = new MyEntity(2L)
          this.dto = new EntityDTO()
  
          repository.save(this.entity1)
          repository.save(this.entity2)
      }
  
      @After
      void clean() {
          List&lt;MyEntity&gt; all = repository.findAll()
          if (all != null || !all.isEmpty()) {
              for (MyEntity entity : all) {
                  repository.delete(entity)
              }
          }
      }
  
      @Unroll
      'findById test'() {
          when:
             def response = restTemplate.getForEntity(url, EntityDTO)
          then:
             response.getStatusCode() == statusCode
          where:
             url              | statusCode
             '/myurl/id/1'    | HttpStatus.OK
             '/myurl/id/2'    | HttpStatus.OK
             '/myurl/id/3'    | HttpStatus.NOT_FOUND
      }
  </code></pre>
  
  <p>And my controller:</p>
  
  <pre><code>@GetMapping(value = "/id/{id}")
      public ResponseEntity&lt;EntityDTO&gt; findById(@PathVariable Long id) {
          final EntityDTO dto = service.findById(id);
          if (dto != null) {
              return new ResponseEntity&lt;EntityDTO&gt;(dto, HttpStatus.OK);
          }
          return new ResponseEntity&lt;EntityDTO&gt;(dto, HttpStatus.NOT_FOUND);
      }
  </code></pre>
  
  <p>And when I run this code I get an error:</p>
  
  <blockquote>
    <p>Condition not satisfied:</p>
    
    <p>response.getStatusCode() == statusCode |        |               |  | |
    404             |  200 |                        false &lt;404 Not
    Found,{Content-Length=[0], Date=[Thu, 15 Jun 2017 16:07:21 GMT]}></p>
  </blockquote>
  
  <p>And when I click 'See diference" I see details: Expected:</p>
  
  <blockquote>
    <p>groovy.lang.MissingFieldException: No such field: name for class:
    org.springframework.http.HttpStatus   at
    groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2820) ... at
    org.spockframework.runtime.JUnitSupervisor.convertToComparisonFailure(JUnitSupervisor.java:135)</p>
  </blockquote>
  
  <p>Actual is the same like Expected with one difference:
  at </p>
  
  <blockquote>
    <p>org.spockframework.runtime.JUnitSupervisor.convertToComparisonFailure(JUnitSupervisor.java:134)</p>
  </blockquote>