I have a class(TempClass) containing a map that uses a static method of another class(UtilClass) to generate a list using this map. For mocking the static method(can't avoid using the static method), I'm using Powermockito. However, I'm getting this exception when I try to run the test.
javassist version used: 3.18.1-GA
java.lang.IllegalStateException: Failed to transform class with name somepackage.UtilClass. Reason: javassist.bytecode.InterfaceMethodrefInfo cannot be cast to javassist.bytecode.MethodrefInfo
it disappears when I remove a sorted
call from the static method
I know it can be resolved by dependency changes mentioned here. But I don't want to change the dependency. My question is more around why is this happening?
This is a simplified example of a code that I'm using.
TempClass.java
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
public class TempClass {
public void execute(){
List<Integer> integerList1 = Lists.newArrayList();
List<Integer> integerList2 = Lists.newArrayList();
integerList1.add(10);
integerList1.add(1);
integerList1.add(4);
integerList2.add(10);
integerList2.add(20);
integerList2.add(30);
Map<String, List<Integer>> stringListMap = Maps.newHashMap();
stringListMap.put("AA", integerList1);
stringListMap.put("AB", integerList2);
List<Integer> sortedList = UtilClass.getSomeList(stringListMap);
}
}
UtilClass.java
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class UtilClass {
public static List<Integer> getSomeList(Map<String, List<Integer>> inputs) {
return inputs.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
}
TempClassTest.java
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({
UtilClass.class
})
public class TempClassTest {
@Test
public void dummy(){
Assert.assertEquals(1, 1);
}
}
The exception disappears and the test runs fine when I remove the sorted
call from the method.
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class UtilClass {
public static List<Integer> getSomeList(Map<String, List<Integer>> inputs) {
return inputs.entrySet().stream()
.map(Map.Entry::getValue)
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
}