title: Transactions in spring boot testing not rolled back
body: <p>I have an integrations test class for my <code>UserController</code>. The contents of the following class are:</p>
  
  <pre><code>// imports...
  
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  @RunWith(SpringRunner.class)
  @Transactional
  @Rollback
  public class UserControllerTests {
  
      private static final String ENDPOINT = "/v1/users";
  
      @Autowired
      private TestRestTemplate restTemplate;
  
      @Autowired
      private ApplicationProperties applicationProperties;
  
      @Test
      public void test_user_create() {
          String token = login("test", "test");
          HttpEntity&lt;UserRequest&gt; request = createRequest(token, "admin", "admin");
          ResponseEntity&lt;User&gt; response = restTemplate.exchange(ENDPOINT, HttpMethod.POST, request, User.class);
  
          assertEquals(HttpStatus.CREATED, response.getStatusCode());
      }
  
      private HttpEntity createRequest(String token) {
          HttpHeaders headers = new HttpHeaders();
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set("Authorization", String.format("Bearer %s", token));
          return new HttpEntity(headers);
      }
  
      private HttpEntity&lt;UserRequest&gt; createRequest(String token, String username, String password) {
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set("Authorization", String.format("Bearer %s", token));
          return new HttpEntity&lt;&gt;(new UserRequest(username, password), headers);
      }
  
      private String login(String username, String password) {
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
          headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
          headers.set("Authorization", String.format("Basic %s", Base64.getEncoder().encodeToString(String.format("%s:%s", applicationProperties.getAuth().getClientId(), applicationProperties.getAuth().getClientSecret()).getBytes())));
          MultiValueMap&lt;String, String&gt; body = new LinkedMultiValueMap&lt;&gt;();
          body.add("grant_type", "password");
          body.add("username", username);
          body.add("password", password);
          HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; request = new HttpEntity&lt;&gt;(body, headers);
          ResponseEntity&lt;OAuth2AccessToken&gt; response = restTemplate.exchange("/oauth/token", HttpMethod.POST, request, OAuth2AccessToken.class);
          return response.getBody().getValue();
      }
  }
  </code></pre>
  
  <p>When I execute this test class twice, the second time it fails because there is already a user in the database with username <code>admin</code> (unique constraint).</p>
  
  <p>I am testing against a <code>postgres</code> database which is the same as in my production environment. The application is using Spring's <code>jdbcTemplate</code> for database operations.</p>
  
  <p>My logging produced the following logs:</p>
  
  <pre><code>2017-10-13 14:11:31.407  INFO [iam-service,,,] 63566 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context 
  ...
  2017-10-13 14:11:32.050  INFO [iam-service,,,] 63566 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test context 
  </code></pre>
  
  <p>My application flow is <code>&lt;request&gt; --&gt; &lt;controller&gt; --&gt; &lt;service with jdbcTemplate&gt;</code> and the services are annotation with <code>@Transactional</code>.</p>
  
  <p>I'm really stuck with this.</p>
  
  <p>One solution found didn't work for me, it was creating a <code>PlatformTransactionManager</code> bean for the test configuration:</p>
  
  <pre><code>@Bean
  public PlatformTransactionManager transactionManager(DataSource dataSource) {
      return new DataSourceTransactionManager(dataSource);
  }
  </code></pre>