使用JUnit 4测试Spring web服务时出错



你好,我正在尝试在Spring中使用JUnit测试我的Web服务。我写了这个测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/applicationContext.xml"})
@TransactionConfiguration
@Transactional
@SuppressWarnings("unused")
public class SiteTest {
@Mock
public IBusiness siteBusiness;
@Autowired
public ManageImpl managesiteI;
List<GeographicSite> sitesJson = null;
@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    siteApplicationImpl=new ManageSiteImpl(siteApplicationBusiness);
}
@Test
public void testSizeOfSIte() throws ExceptionApiV2  {
try {
    when(managesiteI.geographicSitesAPIV2(Mockito.eq((Integer)10), Mockito.eq((Integer)0), Mockito.anyString(), Mockito.anyString(), 
            Mockito.eq("CI00002384"),Mockito.eq("CI00002394"), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), 
            Mockito.anyString(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(resultat);

    } catch (Exception e) {
        System.out.println("ERRO MOCKITO: "+e);
    }
    sitesJson=managesiteI.geographicSitesAPIV2( 10, 0, "site.id", null, "CI00002384" , "CI00002394", null, null, null, null, null, null, null, null, null, null );

我已添加applicationContext.xml添加了我认为需要的所有依赖项是的,它不起作用,首先我得到了一个错误的日志:

log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.

我设法用一个静态方法跳过了它,但我总是出现这样的错误:

Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7a95626d: defining beans [propertyConfigurer,getActor.proxyFactory,getActor,actorBusinessImpl,manageSiteSoapProxy,manageSiteSoapProxyFactory,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionInterceptor,transactionManager,transactionAttributes,autoProxyTransactionCreator,dataSource,siterefEntityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0]; root of factory hierarchy
Caught exception while allowing TestExecutionListener   [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@8fb4c62] to prepare test instance [xxx.xxx.xxx.xxx.xxxx.SiteApplicationTest@2a53ba89]
java.lang.IllegalStateException: Failed to load ApplicationContext
at      org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157) ~[spring-test-3.1.0.RELEASE.jar:3.1.0.RELEASE]

我有这个原因:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' defined in class path resource [META-INF/jpaDaoContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'siterefEntityManagerFactory' defined in class path resource [META-INF/jpaDaoContext.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/resource/spi/XATerminator
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]

编辑

我在上一个问题之前有一个错误:我不认为我有和另一个问题相同的问题;

Caused by: org.springframework.beans.factory.BeanCreationException: Error     creating bean with name 'siterefEntityManagerFactory' defined in class path     resource [META-INF/jpaDaoContext.xml]: Invocation of init method failed; nested     exception is java.lang.NoClassDefFoundError: javax/resource/spi/XATerminator

****编辑2****
添加javax.resource的依赖项后我收到了这个/

Bean 'siterefEntityManagerFactory' of type [class   org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@72ee3d51: defining beans [propertyConfigurer,getActor.proxyFactory,getActor,.....]; root of factory hierarchy
FactoryBean threw exception from getObjectType, despite the contract saying     that it should return null if the type of its object cannot be determined yet`

有人知道吗?我错过了什么?如果你认为我应该详细说明一些事情,请告诉我,谢谢你的回答和评论

嵌套异常为java.lang.NoClassDefFoundError:javax/resource/spi/XATerminator

表示在独立运行测试时,类路径中缺少jar。类路径中似乎缺少javax资源API。如果您正在使用maven,则需要添加此依赖项进行测试。

<dependency>
 <groupId>javax.resource</groupId>
 <artifactId>javax.resource-api</artifactId>
 <version>1.7</version>
 <scope>test</scope>  
</dependency>

相关后

相关内容

  • 没有找到相关文章

最新更新