`
zl198751
  • 浏览: 278466 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring中的Unit Test

阅读更多

方法很简单:

1. 首先RunWith指定是用Spring的Unit Test

2. ContextConfiguration指定读取Spring 配置

3. TransactionConfiguration指定Transaction的管理,defaultRollBack指定数据不回滚,默认是true, 跑完case后数据回滚。

@Test

用于标记TestCase

以下列举出了主要的annotation。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "application-context.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback=false)
public class BaseTest extends AbstractDependencyInjectionSpringContextTests {

    @Autowired
    protected TestDao dao;

     @BeforeTransaction
     public void verifyInitialDatabaseState() {}
     // set up test data within the transaction
     @Before
     public void setUpTestDataWithinTransaction() {}
     @Test
     // overrides the class-level defaultRollback setting
     @Rollback(true)
     public void modifyDatabaseWithinTransaction() {}
     // execute "tear down" logic within the transaction
     @After
     public void tearDownWithinTransaction() {}
     // logic to verify the final state after transaction has rolled back
     @AfterTransaction
     public void verifyFinalDatabaseState() {}
     @Test
     // logic which does not modify database state
     @NotTransactional
     public void performNonDatabaseRelatedAction() {}
    
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics