title: How to test rollback of transaction in Hibernate+Spring+JUnit
body: <p>I am trying to test the mechanism of rollbackin transaction in cases of somthing went wrong. I read many similar topics, but nothing helps me. Here is what i try:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/rest-servlet.xml" })
  @Transactional
  public class TagDaoImplTest extends DatabaseTestCase {
  
      private static final String FLAT_XML_DATASET = "FlatXmlDataSet.xml";
  
      @Autowired
      private TagDao tagDao;
      @Autowired
      private SessionFactory sessionFactory;
  
      @Before
      public void setUp() throws Exception {
          DatabaseOperation.REFRESH.execute(getConnection(), getDataSet());
      }
  
      @Test
      public void testAddWithRollback() throws Exception {
          addWithRollback("testTagToAdd"); //i suppouse that we tried to add, but somthing went wrong and transaction was rolled back
          final Tag testTagToAdd = tagDao.findByTag("testTagToAdd"); // so db must be without changes and i check this
          assertNull(testTagToAdd);
      }
  
      @Rollback(true) //i want this method to rollback after its work to imitate corrupted transaction
      private void addWithRollback(String tag) throws Exception {
          tagDao.add(new Tag(tag));
      }
  }
  </code></pre>
  
  <p>My dao looks like this:</p>
  
  <pre><code>@Repository("tagDao")
  public class TagDaoImpl implements TagDao {
  
      @Override
      public void add(Tag tag) {
          final Session session = sessionFactory.getCurrentSession();
          session.persist(tag);
      }
  }
  </code></pre>
  
  <p>But my test fails because it finds that tag in db (and this means transaction wasn`t rolled back). I tried many different things like get current session and transaction and manually invoke rollback, but nothing happend. Can you please help me?</p>