记Spring5集成TestNG进行单元测试的采坑经历

Posted david1221

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记Spring5集成TestNG进行单元测试的采坑经历相关的知识,希望对你有一定的参考价值。

    TestNG是一款优秀的单元测试工具,和JUinit相比有不少有点,本文不做赘述。本文主要是对自己在配置过程中遇到的问题进行记录,也算是自己的经验积累,也希望有这方面经验的朋友不吝赐教。

一、版本

1、spring   5.0.2.RELEASE

2、TestNG  6.8.7

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            <scope>test</scope>
        </dependency>

3、开始踩坑

3.1

     使用在spring中集成TestNG,单元测试类主要是要继承两个类 

AbstractTestNGSpringContextTests , AbstractTransactionalTestNGSpringContextTests。先说明继承这个类AbstractTestNGSpringContextTests 的情况
@ContextConfiguration("classpath:applicationContext.xml")
public class RoleServiceTest extends AbstractTestNGSpringContextTests 

    @Autowired
    RoleService roleService;

    @Test
    public void testFindAll() 

        Assert.assertEquals(roleService.findAll().size(),3);

    

      这样是没有问题的。下面说说继承 AbstractTransactionalTestNGSpringContextTests 这个类的情况。代码如下:

@ContextConfiguration("classpath:applicationContext.xml")
public class RoleServiceTransctionalTest extends AbstractTransactionalTestNGSpringContextTests
    @Test
    public void testFindAll() 

               
 Assert.assertEquals(JdbcTestUtils.countRowsInTable(this.jdbcTemplate,"sys_role"),3);
       

    

出现如下错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
	at org.springframework.test.context.transaction.TestContextTransactionUtils.retrieveTransactionManager(TestContextTransactionUtils.java:194)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:350)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:331)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:183)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:287)
	at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:157)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
2020-03-18 11:30:50,562 728    [           main] DEBUG on.TestContextTransactionUtils  - Caught exception while retrieving PlatformTransactionManager for test context [DefaultTestContext@74294adb testClass = RoleServiceTransctionalTest, testInstance = org.davidframework.biz2.user.service.RoleServiceTransctionalTest@4fcd19b3, testMethod = testFindAll@RoleServiceTransctionalTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@70a9f84e testClass = RoleServiceTransctionalTest, locations = 'classpath:applicationContext.xml', classes = '', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = '', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]], attributes = map[[empty]]]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1205)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
	at org.springframework.test.context.transaction.TestContextTransactionUtils.retrieveTransactionManager(TestContextTransactionUtils.java:211)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:350)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:331)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:183)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:287)
	at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:157)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
2020-03-18 11:30:50,567 733    [           main] WARN  est.context.TestContextManager  - Caught exception while invoking 'beforeTestMethod' callback on TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@69b0fd6f] for test method [public void org.davidframework.biz2.user.service.RoleServiceTransctionalTest.testFindAll()] and test instance [org.davidframework.biz2.user.service.RoleServiceTransctionalTest@4fcd19b3]
java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test for test context [DefaultTestContext@74294adb testClass = RoleServiceTransctionalTest, testInstance = org.davidframework.biz2.user.service.RoleServiceTransctionalTest@4fcd19b3, testMethod = testFindAll@RoleServiceTransctionalTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@70a9f84e testClass = RoleServiceTransctionalTest, locations = 'classpath:applicationContext.xml', classes = '', contextInitializerClasses = '[]', activeProfiles = '', propertySourceLocations = '', propertySourceProperties = '', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]], attributes = map[[empty]]].
	at org.springframework.util.Assert.state(Assert.java:94)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:184)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:287)
	at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:157)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
	at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
	at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
	at org.testng.TestRunner.privateRun(TestRunner.java:767)
	at org.testng.TestRunner.run(TestRunner.java:617)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
	at org.testng.TestNG.run(TestNG.java:1057)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)

出现这种问题主要是在applicationContext.xml里面没有配置事务管理器,配置了应该就可以解决。

3.2 找不到  SessionCookieConfig 类错误

  出现这个错误的原因的下面的这个包版本太低导致的,这个类在3.1及以上的版本中才有。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

3.2 spring-test的版本问题

 还有的错误是 spring-test5+的包和TestNG包的匹配有问题,换成spring-test4+的版本就可以了

3.3 TestNG的版本问题

如果你的Spring5的版本比较高,尽量采用TestNG的高版本,推荐6.11 或者 7.1.0版本。

3.4 testng 与 com.springsource.org.testng

  在官方的网站上,有这两个包,建议使用 com.springsource.org.testng 这是专门为 spring开发的,匹配性要好一些。

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.7</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>com.springsource.org.testng</artifactId>
    <version>6.5.2</version>
</dependency>

昨天还遇到了好多坑,今天没有重现,也没有截图

 

以上是关于记Spring5集成TestNG进行单元测试的采坑经历的主要内容,如果未能解决你的问题,请参考以下文章

基于UnixSocket抽象命名空间的采坑记录

基于UnixSocket抽象命名空间的采坑记录

如何使用注释在 testng + maven 中分离单元测试和集成测试?

Mac使用Gradle上传jar到中央仓库(最完整的采坑记录)

菜鸟关于js“this”的采坑记录

记一次虚拟机共享文件夹的采坑之旅