将 @Autowired 与 JMockit 一起使用会使 @Autowired 对象为空
我意识到还有另一个问题可以解决这个确切的问题(此处)。但是,它在我的情况下不起作用。
我有一个使用 spring 的 Maven(网络/前端)项目。我已通过 pom 将 jmockit 添加到 jvm:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/mockit/jmockit/0.998/jmockit-0.998.jar</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
<forkMode>always</forkMode>
</configuration>
</plugin>
SUT (缩写)如下所示:
@Service
@RequestMapping("/bars")
public class BarsController{
[...]
@Autowired
private FooUtils fooUtils;
[...]
@RequestMapping(value = "/get", method = RequestMethod.POST)
public ModelAndView getBars(){
ModelAndView mav = new ModelAndView();
Session session = fooUtils.getSession();
[...]
现在,我真的很想在测试中模拟 FooUtils 实例。按照这个SO问题中给出的建议,我尝试了:
@RunWith(JMockit.class)
public class BarsControllerTest {
@Autowired BarsController unitUnderTest;
@Mocked Session session;
@Before
public void setUp()
{
FooUtils foo = new MockUp <FooUtils>() {
@Mock
Session getSession() {
return session;
}
}.getMockInstance();
mockit.Deencapsulation.setField(unitUnderTest, foo);
}
唉,unitUnderTest
以及foo
都是 null
,导致这种情况发生:
java.lang.NullPointerException
at net.manniche.thebars.BarsControllerTest.setUp(BarsControllerTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:78)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
- 这是非常意外的,正如我所期望的
new MockUp<...>{}.getMockInstance( )
返回某个对象。
我想我只是错过了一些关键部分,但是哪一个呢?
I realize that there is another SO question which deals with this exact problem (here). However, it won't work in my case.
I have a maven (web/frontend) project using spring. I've added jmockit to the jvm through the pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/mockit/jmockit/0.998/jmockit-0.998.jar</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
<forkMode>always</forkMode>
</configuration>
</plugin>
The SUT (abbreviated) looks like:
@Service
@RequestMapping("/bars")
public class BarsController{
[...]
@Autowired
private FooUtils fooUtils;
[...]
@RequestMapping(value = "/get", method = RequestMethod.POST)
public ModelAndView getBars(){
ModelAndView mav = new ModelAndView();
Session session = fooUtils.getSession();
[...]
Now, I would really like to mock out the FooUtils
instance in my test. Following the advice given in this SO question, I tried:
@RunWith(JMockit.class)
public class BarsControllerTest {
@Autowired BarsController unitUnderTest;
@Mocked Session session;
@Before
public void setUp()
{
FooUtils foo = new MockUp <FooUtils>() {
@Mock
Session getSession() {
return session;
}
}.getMockInstance();
mockit.Deencapsulation.setField(unitUnderTest, foo);
}
Alas, the unitUnderTest
as well as the foo
are both null
, causing this to happen:
java.lang.NullPointerException
at net.manniche.thebars.BarsControllerTest.setUp(BarsControllerTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:78)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
- Which is quite unexpected, as I would expect
new MockUp<...>{}.getMockInstance()
to return some object.
I guess that I'm just missing out on some crucial part, but which?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论