使用JUnit 4

时间:2016-05-02 15:42:36

标签: java spring junit mockito

您好我试图在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`

任何人都有任何想法?我错过了什么? 如果你认为我应该详细说明一些事情让我知道,谢谢你的回答和评论

1 个答案:

答案 0 :(得分:0)

  

嵌套异常是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>

相关post