日期:2014-05-17  浏览次数:20721 次

在junit中用JSONArray,就会自动停止junit
Java code


package com.ibm.poc.additional;
import net.sf.json.JSONArray;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;


import com.ibm.poc.additional.service.IAdditionalService;
@ContextConfiguration(locations = {"/conf/applicationContext.xml"})
public class AdditionalServiceTest extends AbstractJUnit4SpringContextTests{
    
    @Autowired
    private IAdditionalService additionalService;
    
    
    @Test
    public void test() {
                JSONArray list = new JSONArray();
        additionalService.additionaQuery("", "", "", 1, 2);
    }

}








JSONArray list = new JSONArray();
只要我在junit中加入这句话,运行到这一步的时候junit自动停止,不抛任何异常。只在console打印出一部分的spring信息

[INFO](2012-08-13 13:24:06,609) - Closing org.springframework.context.support.GenericApplicationContext@1ba6076: startup date [Mon Aug 13 13:23:31 CST 2012]; root of context hierarchy
[INFO](2012-08-13 13:24:06,609) - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@27cd63: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,propertyConfigurer,dataSource,transactionManager,defaultJsonResProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,userService,dataInitService,menuItemService,helloHessianImpl,posClaimHospitalService,posClaimHospitalDao,additionalService,addBeDetailListQueryService,addBeDetailListQueryServiceClientFactory,wss4jOutConfiguration,clientPasswordCallback,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy

而且单步调试时,走到这一行按F6,就直接进到org.junit.internal.runners.model.ReflectiveCallable里面去了,这到底是什么问题?




------解决方案--------------------
既然已经用了Spring 那为何还要new对象?是不是Spring IOC的问题?
------解决方案--------------------
Java code

@Runwith("忘记什么.class了")
@ContextConfiguration(locations = {"/conf/applicationContext.xml"})
public class AdditionalServiceTest extends AbstractJUnit4SpringContextTests{
    
    @Autowired
    private IAdditionalService additionalService;
    
    
    @Test
    public void test() {
                JSONArray list = new JSONArray();
        additionalService.additionaQuery("", "", "", 1, 2);
    }

}

------解决方案--------------------
那你就确定不是JSONArray list = new JSONArray()注释了,你的项目跑起来就没得问题?
------解决方案--------------------
LZ我测试过是没有问题的,
<code>
@Test
public void testJsonArray(){
JSONArray array=new JSONArray();

System.out.println("============"); 
}
</code>
你可以去单独写一个测试类,去测试JsonArray看看