如何在Spring Boot中将mybatis-spring-boot-starter-test与TestNG集成?

时间:2018-09-20 03:50:20

标签: java testng spring-boot-test spring-mybatis

如何在Spring Boot中将mybatis-spring-boot-starter-testTestNG集成在一起?

实际上,我们要使用MyBatis + TestNG + Spring-Boot-Starter-Test进行回归测试。当mybatis-spring-boot-starter-testTestNG集成时,我遇到错误,如下所示:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.test.regression.testcase.UserTest': Unsatisfied dependency expressed through field 'conversionProcessor'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.processor.ConversionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:400)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:145)
    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.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
    at org.testng.SuiteRunner.run(SuiteRunner.java:254)
    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 org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.processor.ConversionProcessor' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
    ... 31 more

以下是我遇到错误的代码:

@RunWith(SpringRunner.class)
@SpringBootTest
@org.mybatis.spring.boot.test.autoconfigure.MybatisTest
@org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.test.mybatis.mapper")
public class BusinessModelConversionTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private ConversionProcessor conversionProcessor;
    @Autowired
    private UserMapper userMapper;

    @org.testng.annotations.Test
    public void convertUser() {
         // do something
    }

有用于库集成测试的代码示例:

对于Mybatis + Junit Test来说效果很好:

@RunWith(SpringRunner.class)
@org.mybatis.spring.boot.test.autoconfigure.MybatisTest
@org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.test.mybatis.mapper")
public class MybatisTest {
    @Autowired
    private UserMapper userMapper;

    @org.junit.Test
    public void testMapper() {
        User user = userMapper.findUserByState(LocalDate.now().toString(), 1);
        System.out.println(user);
    }

}

对于TestNG + Spring-Boot-Starter-Test来说效果很好:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest extends AbstractTestNGSpringContextTests {
    @Autowired
    private ConversionProcessor conversionProcessor;

    @org.testng.annotations.Test
    public void convertUser() {
        conversionProcessor.covert();
    }
}

0 个答案:

没有答案