2016-05-02 79 views
0

您好,我正在嘗試在Spring中使用JUnit測試我的webservice。我寫了這個測試:使用JUnit測試Spring Web服務時的錯誤4

@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.s[email protected]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.springframewor[email protected]8fb4c62] to prepare test instance [[email protected]] 
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.s[email protected]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` 

任何一個有什麼想法?我錯過了什麼? ,如果你認爲我應該詳細的東西讓我知道,謝謝你的回答和評論

+0

無ü已編輯的問題,清除了這一點:) –

+0

這是一個錯誤或警告?它應該在日誌中。 – randominstanceOfLivingThing

+0

你可以發佈你的applicationContext.xml嗎? – randominstanceOfLivingThing

回答

0

nested exception is java.lang.NoClassDefFoundError: javax/resource/spi/XATerminator

是你缺少的jar在您的classpath運行測試時,獨立的指示。 您似乎缺少類路徑中的javax資源API。如果你使用的是maven,你需要添加這個依賴項來進行測試。

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

相關post

+0

非常感謝你的回答,實際上我沒有添加這個依賴關係,並且我沒有再得到原來的錯誤,而是我有這個,在第二個編輯中 –