如果将域类作为参数的新方法添加到单元测试中,Spring @Configurable 不起作用
我需要将依赖项注入到我的域类中,因此我使用 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论