簡體   English   中英

使用JUnit 4測試Spring Web服務時出錯

[英]Errors when testing a Spring web service with JUnit 4

您好,我正在嘗試在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>

相關文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM