13
回答
maven3 + struts2 + Spring3 + JPA2(hibernate) 单元测试调用 service 随机出现 空指针错误,有 兄弟碰上过没?
注册华为云得mate10,2.9折抢先购!>>>   

参考 itcast的巴巴运动网,使用maven3 + struts2 + Spring3 + JPA2(hibernate)重构。前两天重构单元测试时,碰上了诡异的空指针错误:

单步调试,单元测试都通过;
运行项目,单元测试报错:

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.541 sec <<< FAILURE!
testSave(com.itcast.service.product.BrandServiceTest)  Time elapsed: 0.002 sec  <<< ERROR!
java.lang.NullPointerException
    at com.itcast.service.product.BrandServiceTest.testSave(BrandServiceTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

诡异的是有时候运行就正常,不报错,有时候就报错。有时候两个测试类只有一个报错,有时候是只有一个测试类的某一个方法报错。都是空指针错误。麻烦高手帮忙瞅瞅哪里出问题了。
琢磨三五天了,没有最终解决掉。

源代码如下:

完整下载:http://www.oschina.net/code/snippet_104958_6526

部分相关代码

BrandServiceTest.java

package com.itcast.service.product;

import com.itcast.model.product.Brand;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = {"classpath:beans.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class BrandServiceTest   {

  private static final Logger logger = Logger.getLogger(BrandServiceTest.class.getName());
  @Resource(name = "brandServiceImpl")
  private BrandService brandService;

  @Test
  public void testSave() {
    Brand brand = new Brand("远阳瑜伽", "/image/brand/2011/10/04/yuanyangyujia.png");
    //System.err.println(brandService);
    //System.err.println(brand.getCode());

      brandService.save(brand);
  }

  @Test
  public void testfind() {
    System.out.println("测试Find()方法!");
    Brand brand = (Brand) brandService.find(Brand.class, "55fba033-b4a7-4d61-89c0-2564b020731c");
    logger.log(Level.INFO, "获取id为\"55fba033-b4a7-4d61-89c0-2564b020731c\"的记录! {0}", brand);


  }
}

BrandServiceImpl.java

package com.itcast.service.product;

import com.itcast.model.product.Brand;
import java.util.UUID;

import com.itcast.service.dao.DaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service  //标注为Spring的Servicebean,供自动扫描机制加载该bean,就不用再beans.xml中声明了
@Transactional
public class BrandServiceImpl extends DaoSupport implements BrandService {

  public BrandServiceImpl() {
  }

  @Override
  public void save(Object entity) {
    ((Brand) entity).setCode(UUID.randomUUID().toString());
    super.save(entity);
  }
}

DaoSupport.java

package com.itcast.service.dao;

import com.itcast.model.QueryResult;
import java.util.LinkedHashMap;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional // 后面的方法都会具有默认的事务行为
public abstract class DaoSupport implements Dao {

  /**
   *
   */
  @PersistenceContext
  protected EntityManager em; // 允许子类访问,不用再在子类中注入

  @Override
  public void save(Object entity) {

    //System.out.println("entityManager====" + em + "caller========" + Reflection.getCallerClass(2));
    em.persist(entity);
  }

  @Override
  public void update(Object entity) {
    em.merge(entity);
  }

  @Override
  public <T> void delete(Class<T> entityClass, Object entityid) {
    delete(entityClass, new Object[]{entityid});
  }

  @Override
  public <T> void delete(Class<T> entityClass, Object[] entityids) {
    for (Object id : entityids) {
      em.remove(em.getReference(entityClass, id));
    }
  }

  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> T find(Class<T> entityClass, Object entityId) {
    //System.out.println("entityManager====" + em);
    return em.find(entityClass, entityId);
  }

  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex,
          int maxresult, String wherejpql, Object[] queryParams, LinkedHashMap<String, String> orderby) {
    QueryResult qr = new QueryResult<T>();
    String entityname = getEntityName(entityClass);
    Query query = em.createQuery("select o from " + entityname + " o " + (wherejpql == null ? "" : "where " + wherejpql) + buildOrderby(orderby));
    setQueryParams(query, queryParams);
    if (firstindex != -1 && maxresult != -1) {
      query.setFirstResult(firstindex);
      query.setMaxResults(maxresult);
      //这两句也可以简写为:query.setFirstResult(firstindex).setMaxResults(maxresult);
    }
    qr.setResultlist(query.getResultList());
    query = em.createQuery("select count(o) from " + entityname + " o " + (wherejpql == null ? "" : "where " + wherejpql));
    setQueryParams(query, queryParams);
    qr.setTotalrecord((Long) query.getSingleResult());

    return qr;
  }

  /**
   *
   * @param <T>
   * @param entityClass
   * @param firstindex
   * @param maxresult
   * @param orderby
   * @return
   */
  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult, LinkedHashMap<String, String> orderby) {
    return getScrollData(entityClass, firstindex, maxresult, null, null, orderby);
  }

  /**
   *
   * @param <T>
   * @param entityClass
   * @param firstindex
   * @param maxresult
   * @param wherejpql
   * @param queryParams
   * @return
   */
  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult, String wherejpql, Object[] queryParams) {
    return getScrollData(entityClass, firstindex, maxresult, wherejpql, queryParams, null);
  }

  /**
   *
   * @param <T>
   * @param entityClass
   * @param firstindex
   * @param maxresult
   * @return
   */
  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> QueryResult<T> getScrollData(Class<T> entityClass, int firstindex, int maxresult) {
    return getScrollData(entityClass, firstindex, maxresult, null, null, null);
  }

  /**
   *
   * @param <T>
   * @param entityClass
   * @return
   */
  @Override
  @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
  public <T> QueryResult<T> getScrollData(Class<T> entityClass) {
    return getScrollData(entityClass, -1, -1);
  }

  /**
   *
   * @param query
   * @param queryParams
   */
  protected void setQueryParams(Query query, Object[] queryParams) {
    if (queryParams != null && queryParams.length > 0) {
      for (int i = 0; i < queryParams.length; i++) {
        query.setParameter(i + 1, queryParams[i]);
      }
    }
  }

  /**
   * 组装order by 语句
   * @param orderby
   * @return
   */
  protected String buildOrderby(LinkedHashMap<String, String> orderby) {
    StringBuilder orderbyql = new StringBuilder();
    if (orderby != null && orderby.size() > 0) {
      orderbyql.append(" order by ");
      // order by o.key desc,o.key2 asc
      for (String key : orderby.keySet()) {
        orderbyql.append("o.").append(key).append(" ").append(orderby.get(key)).append(",");
      }
      orderbyql.deleteCharAt(orderbyql.length() - 1);
    }
    return orderbyql.toString();
  }

  /**
   * 利用反射技术得到要传递到分页方法中的实体类的名字,简单名字还是自定义名字
   * @param <T>
   * @param entityClass
   * @return
   */
  protected <T> String getEntityName(Class<T> entityClass) {
    String entityname = entityClass.getSimpleName();
    Entity entity = entityClass.getAnnotation(Entity.class);
    if (entity.name() != null && !"".equals(entity.name())) {
      entityname = entity.name();
    }

    return entityname;
  }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <context:component-scan base-package="com.itcast"/>
  <context:property-placeholder location="classpath:jdbc.properties"/>
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driverClassName}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}"/>
        <!-- 连接池启动时的初始值  -->
    <property name="initialSize" value="${initialSize}" />
        <!-- 连接池的最大值 -->
    <property name="maxActive" value="${maxActive}"/>
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
    <property name="maxIdle" value="${maxIdle}"/>
        <!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会去申请一些连接,以免洪峰来时来不及申请 -->
    <property name="minIdle" value="${minIdle}" />
  </bean>
    <!-- This will ensure that hibernate or jpa exceptions are automatically translated into
         Spring's generic DataAccessException hierarchy for those classes annotated with Repository
         For example see PersonDaoJpa-->
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    <!-- 类工厂由spring管理 -->
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/><!-- 注入数据源bean到实体管理工厂bean -->
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="loadTimeWeaver"><!-- 运行时植入 -->
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
  </bean>
     <!-- bean post-processor for JPA annotations -->
  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <!-- 事务由spring管理 -->
  <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/><!-- 注入实体管理工厂bean到事务管理bean -->
  </bean>
  <tx:annotation-driven transaction-manager="txManager"/><!-- 事务声明方式是注解 -->
<!--    <bean id="producttypelist" class="com.itcast.web.action.product.ProductTypeAction" />-->

</beans>

 

 

 

 

 

 

举报
蛮大人
发帖于6年前 13回/4K+阅

以下是问题补充:

  • @蛮大人 :国外的兄弟已经给指出来问题所在了:after inspecting your full sources: remove: <configuration> <parallel>methods</parallel> <threadCount>10</threadCount> </configuration> ... will make it work. It seems if the tests are executed in parallel, it fails. (6年前)
  • @蛮大人 :--父工程的测试插件并行配置问题 (6年前)
  • @蛮大人 :附上国外的链接:stackoverflow //请无视我快要忘记的英文:) http://stackoverflow.com/questions/7724830/why-i-got-null-point-error-in-the-second-method-when-use-junit-to-test-class (5年前)
共有13个答案 最后回答: 6年前

你运行的是集成测试。和运行环境有关。测试出错后难查。

空指针是因为spring没有注入成功
建议这样的小功能做单元测试。使用mockito模拟吧。

引用来自“李渊”的答案

你运行的是集成测试。和运行环境有关。测试出错后难查。

空指针是因为spring没有注入成功
建议这样的小功能做单元测试。使用mockito模拟吧。

谢谢ls的回复。

很诡异的是空指针随机出现。

两个单元测试类,可能两个都会在调用service时报空指针错误;

也可能之只有一个报空指针错误;

还有一种最奇怪的现象,只有一个方法报空指针,其他都测试通过;比如 这个 testSave;

再贴一段运行log,帮忙一起分析下:

cd D:\MavenProjects\babasportrefactoring\babasportbluetripe; JAVA_HOME=D:\\program\\jdk1.6 D:\\program\\apache-maven-3.0.3\\bin\\mvn.bat -Dnetbeans.deploy=true -Dnetbeans.deploy.clientUrlPart=/ package
Scanning for projects...
                                                                        
------------------------------------------------------------------------
Building babasportbluetripe 1.0-SNAPSHOT
------------------------------------------------------------------------

[resources:resources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 24 resources

[compiler:compile]
Compiling 27 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\classes

[resources:testResources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 10 resources

[compiler:testCompile]
Compiling 2 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\test-classes

[surefire:test]
Surefire report directory: D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Concurrency config is parallel='methods', perCoreThreadCount=true, threadCount=10, useUnlimitedThreads=false
2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.BrandServiceTest]: using defaults.
2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.TypeServiceTest]: using defaults.
Running com.itcast.service.product.TypeServiceTest
测试Find()方法!
2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testFind
信息: 获取id为3的记录 com.itcast.model.product.Type@650646
测试Delete()方法!
测试Update()方法!
2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testUpdate
信息: 更新 id为3的记录 com.itcast.model.product.Type@1b59919
当前产品类别名称为:6篮球产品;其id为 7
当前产品类别名称为:7篮球产品;其id为 8
当前产品类别名称为:8篮球产品;其id为 9
当前产品类别名称为:9篮球产品;其id为 10
当前产品类别名称为:10篮球产品;其id为 11
当前产品类别名称为:11篮球产品;其id为 12
当前产品类别名称为:12篮球产品;其id为 13
当前产品类别名称为:13篮球产品;其id为 14
当前产品类别名称为:14篮球产品;其id为 15
当前产品类别名称为:15篮球产品;其id为 16
测试save()方法!
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.22 sec
Running com.itcast.service.product.BrandServiceTest
2011-10-8 20:37:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@30e280: startup date [Sat Oct 08 20:37:54 CST 2011]; root of context hierarchy
2011-10-8 20:37:54 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'dataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#39443f' of type [class org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:54 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
信息: Building JPA container EntityManagerFactory for persistence unit 'itcast'
2011-10-8 20:37:55 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:55 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9cbd4b: defining beans [brandService,typeService,uploadFileService,/control/product/allbrandsfinder,/control/product/alltypesfinder,/control/product/branddeleter,/control/product/brandfinder,/control/product/brandsaver,/control/product/brandupdater,/control/product/typedeleter,/control/product/typefinder,/control/product/typesaver,/control/product/typeupdater,/control/upload/uploadfilesaver,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.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
com.itcast.service.product.BrandServiceImpl@10952e8
null
测试Find()方法!
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.465 sec <<< FAILURE!

Results :

Tests in error: 
  testfind(com.itcast.service.product.BrandServiceTest)

Tests run: 7, Failures: 0, Errors: 1, Skipped: 0

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 8.499s
Finished at: Sat Oct 08 20:37:56 CST 2011
Final Memory: 18M/254M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project babasportbluetripe: There are test failures.

Please refer to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports for the individual test results.
-> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

再提供一份正常运行部署的日志。很是诡异,空指针随机出现的。

我这里新增打印了em到控制台。

cd D:\MavenProjects\babasportrefactoring\babasportbluetripe; JAVA_HOME=D:\\program\\jdk1.6 D:\\program\\apache-maven-3.0.3\\bin\\mvn.bat -Dnetbeans.deploy=true -Dnetbeans.deploy.clientUrlPart=/ package
Scanning for projects...
                                                                        
------------------------------------------------------------------------
Building babasportbluetripe 1.0-SNAPSHOT
------------------------------------------------------------------------

[resources:resources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 24 resources

[compiler:compile]
Compiling 2 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\classes

[resources:testResources]
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory D:\MavenProjects\babasportrefactoring\babasportbluetripe\src\test\resources

[compiler:testCompile]
Nothing to compile - all classes are up to date

[surefire:test]
Surefire report directory: D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Concurrency config is parallel='methods', perCoreThreadCount=true, threadCount=10, useUnlimitedThreads=false
2011-10-9 23:09:48 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.BrandServiceTest]: using defaults.
2011-10-9 23:09:48 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.TypeServiceTest]: using defaults.
Running com.itcast.service.product.BrandServiceTest
2011-10-9 23:09:48 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2011-10-9 23:09:48 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@12a54f9: startup date [Sun Oct 09 23:09:48 CST 2011]; root of context hierarchy
2011-10-9 23:09:49 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
2011-10-9 23:09:49 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'dataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-9 23:09:49 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#da4b71' of type [class org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-9 23:09:49 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
信息: Building JPA container EntityManagerFactory for persistence unit 'itcast'
2011-10-9 23:09:50 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-9 23:09:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18825b3: defining beans [brandServiceImpl,typeServiceImpl,uploadFileServiceImpl,/control/product/allbrandsfinder,/control/product/alltypesfinder,/control/product/branddeleter,/control/product/brandfinder,/control/product/brandsaver,/control/product/brandupdater,/control/product/typedeleter,/control/product/typefinder,/control/product/typesaver,/control/product/typeupdater,/control/upload/uploadfilesaver,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.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
003测试Find()方法!
004com.itcast.service.product.BrandServiceImpl@180a8b7
entityManager=find()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
00555fba033-b4a7-4d61-89c0-2564b020731c
2011-10-9 23:09:50 com.itcast.service.product.BrandServiceTest testfind
信息: 获取id为"55fba033-b4a7-4d61-89c0-2564b020731c"的记录! com.itcast.model.product.Brand@42a6eb
001com.itcast.service.product.BrandServiceImpl@180a8b7
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
0023e0e03a2-5629-4f43-a7d5-9b07ae85aff7
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.244 sec
Running com.itcast.service.product.TypeServiceTest
测试Find()方法!
entityManager=find()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
2011-10-9 23:09:50 com.itcast.service.product.TypeServiceTest testFind
信息: 获取id为3的记录 com.itcast.model.product.Type@1f2edd2
测试save()方法!
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
entityManager=save()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
测试Update()方法!
entityManager=find()===Shared EntityManager proxy for target factory [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@1115152]
2011-10-9 23:09:51 com.itcast.service.product.TypeServiceTest testUpdate
信息: 更新 id为3的记录 com.itcast.model.product.Type@d510e8
测试Delete()方法!
当前产品类别名称为:6篮球产品;其id为 7
当前产品类别名称为:7篮球产品;其id为 8
当前产品类别名称为:8篮球产品;其id为 9
当前产品类别名称为:9篮球产品;其id为 10
当前产品类别名称为:10篮球产品;其id为 11
当前产品类别名称为:11篮球产品;其id为 12
当前产品类别名称为:12篮球产品;其id为 13
当前产品类别名称为:13篮球产品;其id为 14
当前产品类别名称为:14篮球产品;其id为 15
当前产品类别名称为:15篮球产品;其id为 16
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.988 sec

Results :

Tests run: 7, Failures: 0, Errors: 0, Skipped: 0


[war:war]
Packaging webapp
Assembling webapp [babasportbluetripe] in [D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\babasportbluetripe-1.0-SNAPSHOT]
Processing war project
Copying webapp resources [D:\MavenProjects\babasportrefactoring\babasportbluetripe\src\main\webapp]
Webapp assembled in [729 msecs]
Building war: D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\babasportbluetripe-1.0-SNAPSHOT.war
Warning: selected war files include a WEB-INF/web.xml which will be ignored 
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 12.145s
Finished at: Sun Oct 09 23:09:55 CST 2011
Final Memory: 12M/254M
------------------------------------------------------------------------
NetBeans: Deploying on GlassFish Server 3.x
    profile mode: false
    debug mode: false
    force redeploy: true
就地在 D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\babasportbluetripe-1.0-SNAPSHOT 处部署

 

引用来自“游客”的答案

不用框架已很多年。

俺也不用很多年了。

不过,现在又回到学习使用框架了。

世界就是一个大大的轮回,你无法知道什么时候又走回原点了。

引用来自“蛮大人”的答案

引用来自“游客”的答案

不用框架已很多年。

俺也不用很多年了。

不过,现在又回到学习使用框架了。

世界就是一个大大的轮回,你无法知道什么时候又走回原点了。

我是坚决不会再走回去的。

引用来自“游客”的答案

引用来自“蛮大人”的答案

引用来自“游客”的答案

不用框架已很多年。

俺也不用很多年了。

不过,现在又回到学习使用框架了。

世界就是一个大大的轮回,你无法知道什么时候又走回原点了。

我是坚决不会再走回去的。
真正的高手也罢,后会有期!

重构后的代码,单独运行测试文件,都没问题。两个测试类一起运行的时候(测试项目)随机报空指针错误。运行的时候也默认会运行所有测试类的,重构后的代码也是随机空指针。

----------------

再传一份重构之前的代码,这个代码怎么运行测试都没问题。来自传智播客黎活明老师的视频的学习。  

 

/*
 * Copyright 2011 待到道成日,纵横天地间.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itcast.service.product;

import com.itcast.model.product.Brand;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 *
 * @author Kang.Cunhua
 */
//@ContextConfiguration(locations = {"classpath:beans.xml"})
//@RunWith(SpringJUnit4ClassRunner.class)
public class BrandServiceTest {

  private static final Logger logger = Logger.getLogger(BrandServiceTest.class.getName());
  private static ApplicationContext cxt;
  private static BrandService brandService;

  @BeforeClass
  public static void setUpClass() throws Exception {
    cxt = new ClassPathXmlApplicationContext("beans.xml");
    brandService = (BrandService) cxt.getBean("brandServiceImpl");

  }
//  @Resource(name = "brandServiceImpl")
//  private BrandService brandService;//= new BrandServiceImpl();

  @AfterClass
  public static void tearDownClass() throws Exception {
  }

  @Before
  public void setUp() {
  }

  @After
  public void tearDown() {
  }

  @Test
  public void testSave() {
    Brand brand = new Brand("远阳瑜伽", "/image/brand/2011/10/04/yuanyangyujia.png");
    System.err.println("001" + brandService);

    brandService.save(brand);

    System.err.println("002" + brand.getCode());
  }

  @Test
  public void testfind() {
    System.out.println("003" + "测试Find()方法!");
    System.err.println("004" + brandService);
    Brand brand = (Brand) brandService.find(Brand.class, "55fba033-b4a7-4d61-89c0-2564b020731c");
    System.err.println("005" + brand.getCode());
    logger.log(Level.INFO, "获取id为\"55fba033-b4a7-4d61-89c0-2564b020731c\"的记录! {0}", brand);


  }
}

引用来自“蛮大人”的答案

引用来自“游客”的答案

引用来自“蛮大人”的答案

引用来自“游客”的答案

不用框架已很多年。

俺也不用很多年了。

不过,现在又回到学习使用框架了。

世界就是一个大大的轮回,你无法知道什么时候又走回原点了。

我是坚决不会再走回去的。
真正的高手也罢,后会有期!

一个应用,引入的jar包近百兆,这种日子已一去不复返。

一个发布的应用,坚决控制在10M以内,包括jar包。

引用来自“游客”的答案

引用来自“蛮大人”的答案

引用来自“游客”的答案

引用来自“蛮大人”的答案

引用来自“游客”的答案

不用框架已很多年。

俺也不用很多年了。

不过,现在又回到学习使用框架了。

世界就是一个大大的轮回,你无法知道什么时候又走回原点了。

我是坚决不会再走回去的。
真正的高手也罢,后会有期!

一个应用,引入的jar包近百兆,这种日子已一去不复返。

一个发布的应用,坚决控制在10M以内,包括jar包。

中小型应用还可以控制在10M之内。大型项目有点困难吧。不用框架的话,项目开发成本会急剧上升,代码质量也不能保证,对于中大型应用来说。不过,如果是一个人开发的话,无所谓用不用框架,看自己习惯。

用maven管理jar包很方便的。打包源代码也没多大的。


顶部