Как интегрировать mybatis-spring-boot-starter-test с TestNG в Spring Boot? - PullRequest
0 голосов
/ 20 сентября 2018

Как интегрировать mybatis-spring-boot-starter-test с TestNG в Spring Boot?

На самом деле, мы хотим использовать MyBatis + TestNG + Spring-Boot-Starter-Test для проведения регрессионного теста.и я встретил ошибку, когда mybatis-spring-boot-starter-test интегрируется с TestNG, как показано ниже:

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();
    }
}
...