jUnit
1. pom.xml
<!-- spring3.2之前需要加下面的依赖 --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm-all</artifactId> <version>2.1</version> </dependency>
2. application.xml 中配置 transactionManager
<!-- 配置事务管理器 --> <!-- 或者 org.springframework.jdbc.datasource.DataSourceTransactionManager --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>
3. AbstractTestCast.java
1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration("/applicationContext.xml") 3 @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) 4 @Transactional 5 public class AbstractTestCase { 6 protected static final Logger log = LoggerFactory.getLogger(AbstractTestCase.class); 7 8 @Before 9 public void init() { 10 // ... 11 } 12 13 public static void main(String[] args) { 14 15 } 16 17 }
4. XXXTest.java
1 import org.junit.After; 2 import org.junit.Before; 3 import org.junit.Test; 4 import static org.junit.Assert.*; 5 6 public class ZXTest extends AbstractTestCase { 7 8 @Before 9 public void login() { 10 assertNotNull(""); 11 } 12 13 @After 14 public void logout() { 15 16 } 17 18 @Test 19 public void testChangePassword() { 20 21 } 22 23 }
别忘了这一行 : import static org.junit.Assert.*;