0

クラス A のプライベート フィールドを変更したいのですが、これは別のクラス B の内部クラスです。Java Reflect パッケージを試してみたところ、そのプライベート フィールドを取得できましたが、それをモック オブジェクトに割り当てると、問題が発生しました。そのフィールドを正しいクラスに設定できません

ここに私のコードがあります:

public class Aggregator {
 public static class AMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
    private LookupService lookupService = null;  <-- field to mock
    ...
  }
}

私のテストコード:

aggregator = new Aggregator();
for (Class<?> c: aggregator.getClass().getClasses()) {
     if (c.getName().equals("Aggregator$AMapper")) {
         Field field = c.getDecalredField("lookupService");
         field.setAccessible(true);
         field.set(c, Mocito.mock(LookupService.class));  <-- failed here
         break;
     }
 }

私が得たエラーメッセージは次のとおりです。

java.lang.IllegalArgumentException: Can not set com.maxmind.geoip.LookupService field Aggregator$EventTypeMapper.lookupService to java.lang.Class..

私のステップの何が問題なのかわかりません。誰か助けてくれますか? ありがとう。

4

1 に答える 1