AbstractTransactionalJUnit4SpringContextTests: cannot get dao to search for inserted data

I am trying to set up integration tests using the base class AbstractTransactionalJUnit4SpringContextTests. My goal is very simple: insert some data into the database with simpleJdbcTemplate, read it with DAO and drop everything back. JPA-> Hibernate is a save layer.

For my tests, I created a database version that has no foreign keys. This should speed up testing by reducing the number of instrument settings for each test; At this stage, I am not interested in testing the integrity of the database, only the business logic in my HQL.

/* DAO */

@Transactional
@Repository("gearDao")
public class GearDaoImpl implements GearDao {
    @PersistenceContext
    private EntityManager entityManager;

    /* Properties go here */

    public Gear findById(Long id) {
        return entityManager.find(Gear.class, id);
    }
}

/* Test Page */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/dom/app/dao/DaoTests-context.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
public class GearDaoImplTests extends AbstractTransactionalJUnit4SpringContextTests   {

    @Autowired
    private GearDao gearDao;

    @Test
    @Rollback(true)
    public void quickTest() {
        String sql;
        // fields renamed to protect the innocent :-)
        sql = "INSERT INTO Gear (Gear_Id, fld2, fld3, fld4, fld5, fld6, fld7) " +
              " VALUES (?,?,?,?,?,?,?)";
        simpleJdbcTemplate.update(sql, 1L, 1L, 1L, "fld4", "fld5", new Date(), "fld7");
        assertEquals(1L, simpleJdbcTemplate.queryForLong("select Gear_Id from Gear where Gear_Id = 1"));
        System.out.println(gearDao);

        Gear gear = gearDao.findById(1L);
        assertNotNull("gear is null.", gear);  // <== This fails.
    }
}

The app (Spring MVC site) works great with DAO. What can happen? And where will I start looking for a solution?

  • DAO - , simpleJdbcTemplate. , , , DaoTests-context.xml .
  • Hibernate , , Gear. , , fld2/fld3/fld4.
  • DAO . JdbcTemplate ? , .
  • . ?
+3
1

. : " , , ". , , , hibernate . , .

. , !

+2

Source: https://habr.com/ru/post/1735381/


All Articles