2

この正確な問題を扱う別の SO の質問があることを認識しています ( here )。ただし、私の場合はうまくいきません。

春を使用したMaven(Web /フロントエンド)プロジェクトがあります。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 questionで与えられたアドバイスに従って、私は試しました:

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

残念ながら、unitUnderTestfooは bothnullであり、これが発生します。

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()を返すと予想していたので、これはまったく予想外です。

重要な部分を見逃しているだけだと思いますが、どれですか?

4

0 に答える 0