如果将具有域类作为参数的新方法添加到单元测试中,则Spring @Configurable不起作用

时间:2011-06-07 15:06:50

标签: aspectj spring-aop

我需要将依赖项注入我的域类,所以我使用@Configurable,如下所示:

@Configurable(preConstruction=true,dependencyCheck=true)
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="TBL_COMPANY")
class Company
{
   @Id
   private long id;

   @Autowired
   private transient IIdentityQueryService identityQueryService;

   Company()
   {
     Assert.notNull(identityQueryService, "IIdentityQueryService was not injected");
   }
}

我的aop.xml:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-verbose">
        <include within="com.xl.nrs.identity.*"/>
        <exclude within="*..*CGLIB*"/>
        <exclude within="*..*javassist*"/>
        <exclude within="*..*DTO*"/>
        <exclude within="*..*Service*"/>
        <exclude within="*..*Test*"/>
    </weaver>
</aspectj>

我在应用程序上下文文件中包含以下内容:

...
<context:spring-configured/>
<context:load-time-weaver/>
...

JVM参数:

-javaagent:../NRS-SharedLibs/lib/org.springframework.instrument-3.0.5.RELEASE.jar

最后,我的简化单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/mycontext.xml"})
public class TestCompany
{
   @Test
   public void testCreateCompany() throws Exception
   {
      Company company = new Company();
   }
}

一切顺利,直到我将新方法添加到单元测试中,并将域类作为其参数之一:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/mycontext.xml"})
public class TestCompany
{
   @Test
   public void testCreateCompany() throws Exception
   {
      Company company = new Company();
   }

   //New method that makes @Configurable not working
   private void someNewMethod(Company company)
   {
      ...
   }
}

这次依赖不会被注入,没有错误,没有任何东西。

如果新方法的参数不是域类,那么一切都再好了。

这很奇怪,因为我只将方法添加到单元测试类,而不是域类或其他任何地方。

有没有人遇到同样的问题?

非常感谢任何帮助。

谢谢&amp;的问候,

Setya

0 个答案:

没有答案