开发者

Using @Autowired with JMockit makes the @Autowired object null

开发者 https://www.devze.com 2023-03-26 11:31 出处:网络
I realize that there is another SO question which deals with this exact problem (here). However, it won\'t work in my case.

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 unexpe开发者_如何学Gocted, 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消