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

发布于 2024-11-14 07:24:22 字数 2125 浏览 0 评论 0原文

我需要将依赖项注入到我的域类中,因此我使用 @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)
   {
      ...
   }
}

这次依赖项不会被注入,没有错误,什么也没有。

如果新方法的参数不是域类,一切都会再次正常。

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

有人遇到过同样的问题吗?

任何帮助将不胜感激。

谢谢&问候,

塞蒂亚

I need to inject dependencies into my domain classes, so I use @Configurable as follows:

@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");
   }
}

My 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>

I include the following in my application context file:

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

JVM parameter:

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

Lastly, my simplified unit test:

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

Everything went fine until I add new method to my unit test with the domain class as one of its parameters:

@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)
   {
      ...
   }
}

This time the dependency wouldn't get injected, no errors, no nothing.

If the parameter of the new method was not the domain classes everything went fine again.

It's so strange because I only added method to the unit test class, not the domain class or anywhere else.

Has anyone experienced the same problem ?

Any help would be greatly appreciated.

Thanks & Regards,

Setya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文