Specs2规范Spring Autowire

时间:2015-11-09 19:37:00

标签: spring scala unit-testing specs2

我想使用Specs2测试框架进行测试,但是当我尝试将它与Spring集成时,我得到了一个java.lang.reflect.MalformedParameterizedTypeException。

有一个specs2-spring library,但这是使用2.10和旧版本的specs2。我查看了this question about scalaTest,它让我看到了这段代码:

@ContextConfiguration(Array("classpath:applicationContext.xml"))
class TryItTest extends SpecificationWithJUnit{

  @Autowired val db:DriverManagerDataSource = null
  new TestContextManager(this.getClass()).prepareTestInstance(this)

  "this thing should" should{
    "run ok" in {
      db.getConnection
      success
    }
  }
}

但是当我尝试运行测试时,我得到了:

Nov 09, 2015 2:11:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
    Nov 09, 2015 2:11:31 PM org.springframework.context.support.GenericApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.GenericApplicationContext@243d315f: startup date [Mon Nov 09 14:11:31 EST 2015]; root of context hierarchy
    Nov 09, 2015 2:11:31 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
    INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
    Nov 09, 2015 2:11:31 PM org.springframework.test.context.TestContextManager prepareTestInstance
    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@131746d9] to prepare test instance [my.package.TryItTest@12474a03]
    java.lang.reflect.MalformedParameterizedTypeException
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments(ParameterizedTypeImpl.java:58)
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.<init>(ParameterizedTypeImpl.java:51)
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.make(ParameterizedTypeImpl.java:92)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeParameterizedType(CoreReflectionFactory.java:105)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:140)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.repository.ConstructorRepository.getParameterTypes(ConstructorRepository.java:94)
    at java.lang.reflect.Executable.getGenericParameterTypes(Executable.java:284)
    at java.lang.reflect.Method.getGenericParameterTypes(Method.java:282)
    at java.beans.FeatureDescriptor.getParameterTypes(FeatureDescriptor.java:387)
    at java.beans.MethodDescriptor.setMethod(MethodDescriptor.java:116)

如果我从SpecificationWithJUnit切换到SpecWithJUnit,它会毫无错误地运行测试,但我不希望手动包含所有这些特征。我认为(至少)Matchers中的一个特征导致了这个问题。有没有其他人试图用Specs2做到这一点?我错过了一些重要的事情吗?

(我使用的是Spring V4.1.0.RELEASE)

1 个答案:

答案 0 :(得分:1)

知道了。我没有使用最新版本的scalaz和specs2。现在一切都很完美。