撤消对javax.persistence.Entity对象的更改

时间:2016-07-11 08:38:46

标签: java spring hibernate jpa junit

我有一个User实体和相应的DAO和impl。 UserDAOImpl中的一种方法是:

@Repository
@Transactional
public class UserDAOImpl implements UserDAO {
  ....
  public void editUser(User user) {
      System.out.println("tttt: " + user);
      if ((user.getUserid() == BigInteger.ONE) && !user.getUsername().equalsIgnoreCase("admin")) {
        em.detach(user);
        throw new IllegalArgumentException ("Cannot change username property for the user admin");
    }
    if ((user.getUserid() == BigInteger.ONE) || !user.getIsActive().toString().equalsIgnoreCase("Yes")) {
        em.detach(user);
        throw new IllegalArgumentException ("Cannot change isactive property for the user admin");
    }
    if ((user.getUserid() == BigInteger.ONE) || !user.getRole().toString().equalsIgnoreCase("Administrator")) {
        em.detach(user);
        throw new IllegalArgumentException ("Cannot change role property for the user admin");
    }
    em.merge(user);
  }
  ....
}

测试此方法的各个Junit代码块是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml",
                               "classpath:/META-INF/spring/applicationContext.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UserDaoTest {
  ....
  @Test
  public void testEditAdminIsactive() {
    User user = userDao.findByUsername("admin");
    user.setIsActive(YNflag.valueOf("No"));
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Cannot change isactive property for the user admin");
    userDao.editUser(user);
  }

  @Test
  public void testEditAdminRole() {
    User user = userDao.findByUsername("admin");
    user.setRole(UserRole.valueOf("Student"));
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Cannot change role property for the user admin");
    userDao.editUser(user);
  }

  @Test
  public void testEditAdminUsername() {
    User user = userDao.findByUsername("admin");
    user.setUsername("admin1");
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Cannot change username property for the user admin");
    userDao.editUser(user);
  }
  ....
}

上下文文件是:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd">

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="persistenceXmlLocation" value="classpath*:META-INF/test-persistence.xml"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sharedEntityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/fsms?useSSL=false"/>
    <property name="username" value="fsms"/>
    <property name="password" value="fsms"/>
  </bean>
</beans>

持久性配置是:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.rsa.projects.webtools.sfsms.model.User</class>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    </properties>
  </persistence-unit>
</persistence>

当我执行Junit测试时,我得到输出:

  

java.lang.AssertionError:

     

tttt:{Userid:1,用户名:admin,全名:管理员,Isactive:不,角色:管理员,地址:,密码:fsms,PhoneNumber:911234567890}

     

tttt:{Userid:1,Username:admin,Fullname:Administrator,Isactive:Yes,Role:Student,Address:,Password:fsms,PhoneNumber:911234567890}

     

tttt:{Userid:1,Username:admin1,Fullname:Administrator,Isactive:Yes,Role:Administrator,Address:,Password:fsms,PhoneNumber:911234567890}

     

testEditAdminRole(com.rsa.projects.webtools.sfsms.test.UserDaoTest)   经过的时间:0.043秒<&lt;&lt; FAILURE!

     

预期:( java.lang.IllegalArgumentException和异常的实例,消息包含&#34的字符串;无法更改角色   用户admin&#34;)的属性        但是:带有消息的异常包含&#34;不能更改用户admin&#34;的角色属性。消息是&#34;无法更改isactive   用户admin&#34;

的属性

这是因为testEditAdminUsernametestEditAdminIsactive中调用的任何设置方法也可以在testEditAdminRole中使用。

如何在User方法中抛出异常之前回滚对editUser实体对象所做的更改?

0 个答案:

没有答案