JUnit没有启动事务,在Spring,JPA下

时间:2015-01-05 13:22:27

标签: java hibernate jpa junit spring-transactions

我在线跟踪了这些例子,但仍然没有任何反应。我通过spring注入了DAO,我正确配置了事务管理器。我的单元情况只是冻结,在调试模式下,我看到调用了before方法,但测试方法永远不会启动。

JUnit的:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "testTxMan")
@ContextConfiguration(classes = { PlentyConfig.class,
    TestHibernateContext.class }, initializers = ConfigFileApplicationContextInitializer.class)
public class TestHibernateFieldDao {

@Autowired
private HibernateTestHelper testHelper;

@Autowired
private HibernateFarmDao farmDao;

@Autowired
private HibernateFieldDao fieldDao;

@Autowired
private HibernateInterestDao interestDao;

@Autowired
private HibernateOrganizationDao organizationDao;

@Autowired
private HibernateOrganizationTypeDao organizationTypeDao;


@Before
public void setUp() throws Exception {
    testHelper.executeQuery("DELETE FROM INTERESTS");
    testHelper.executeQuery("ALTER TABLE INTERESTS AUTO_INCREMENT = 1");
    testHelper.executeQuery("DELETE FROM ORGANIZATIONS");
    testHelper.executeQuery("ALTER TABLE ORGANIZATIONS AUTO_INCREMENT = 1");
    testHelper.executeQuery("DELETE FROM ORGANIZATION_TYPES");
    testHelper
            .executeQuery("ALTER TABLE ORGANIZATION_TYPES AUTO_INCREMENT = 1");
    testHelper.executeQuery("DELETE FROM FIELDS");
    testHelper.executeQuery("ALTER TABLE FIELDS AUTO_INCREMENT = 1");
    testHelper.executeQuery("DELETE FROM FARMS");
    testHelper.executeQuery("ALTER TABLE FARMS AUTO_INCREMENT = 1");

}

@Test
public void testInsertField() {
    Farm farm = new Farm("DummyFarmName");
    farm = farmDao.insert(farm, Farm.class);
    Field field = new Field(farm, "DummyFieldName", true, 0,
            "DummyRotationName");
    Field testField = fieldDao.insert(field, Field.class);

    assertNotNull(testField);
    assert (testField.getId() == 1);
    assertEquals("DummyRotationName", testField.getRotation());
    assertEquals(farm.getId(), testField.getFarm().getId());
    assertNotNull(testField.getCreatedDate());
    assertNotNull(testField.getLastModifiedDate());
}

测试背景:

@Configuration
@ActiveProfiles("development")
@EnableTransactionManagement
public class TestHibernateContext {

@Autowired
public PlentyConfig plentyConfig;

@Bean(name = "dataSource")
@Autowired
public DataSource dataSource(PlentyConfig plentyConfig) {
    final DataSource ds = new DataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://" + plentyConfig.getHostname() + ":"
            + plentyConfig.getDbport() + "/" + plentyConfig.getDbname());
    ds.setUsername(plentyConfig.getUsername());
    ds.setPassword(plentyConfig.getPassword());
    return ds;
}

@Bean
@Autowired
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
        DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource);
    factory.setPackagesToScan("com.rdthree.plenty.domain");
    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.setShowSql(true);
    adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
    factory.setJpaVendorAdapter(adapter);
    return factory;
}

@Bean(name = "testTxMan")
public PlatformTransactionManager platformTransactionManager(
        EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager manager = new JpaTransactionManager();
    manager.setEntityManagerFactory(entityManagerFactory);
    return manager;
}

@Bean(name = "sessionFactory")
public SessionFactory sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(
            dataSource);

    builder.scanPackages("com.rdthree.plenty.domain");

    builder.setProperty("hibernate.show_sql", "true");

    return builder.buildSessionFactory();
}

@Bean
public HibernateTestHelper HibernateTestHelper(SessionFactory sessionFactory) {
    return new HibernateTestHelper(sessionFactory);
}

// All the Daos...

我的道:

@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class HibernateFieldDaoBean extends HibernateGeneralDaoBean<Field>
    implements HibernateFieldDao {

@PersistenceContext
private EntityManager entityManager;

@Override
public List<Field> getAllfieldsByFarm(long farmId) {
    @SuppressWarnings("unchecked")
    List<Field> fields = Collections.checkedList(
            entityManager.createQuery(
                    "FROM Field F WHERE F.farm.id = "
                            + String.valueOf(farmId)).getResultList(),
            Field.class);
    return fields;
}

@Override
public List<Field> getAllFieldsByFarmAndOrganization(long farmId,
        long organizationId) {
    @SuppressWarnings("unchecked")
    List<Field> fields = Collections.checkedList(
            entityManager.createNativeQuery(
                    "SELECT a.ID, a.FARM_ID, a.NAME, a.HIDDEN, a.ACRES, a.ROTATION, a.CREATED, " +
            "a.LAST_MODIFIED FROM FIELDS a, INTERESTS b " +
            "WHERE a.FARM_ID = :farmId AND a.ID = b.FIELD_ID AND b.ORGANIZATION_ID = :organizationId " +
            "ORDER BY NAME", Field.class)
            .setParameter("organizationId", organizationId)
            .setParameter("farmId", farmId)
            .getResultList(), Field.class);
    return fields;
}

}

0 个答案:

没有答案