我有一个名为AbstractAccessManager
的课程,它在 Reflection API 的帮助下为我提供了课程实例。
AbstractAccessManager
的结构如下:
public class AbstractAccessManager{
public static HashMap<String, String> accessManagerPackageMap;
static
{
fillMap();
}
public static HashMap<String, String> fillMap()
{
accessManagerPackageMap = new HashMap<String, String>();
accessManagerPackageMap.put("Bhavik", "com.os.bhavik.");
return accessManagerPackageMap;
}
public static AccessManagerHandler getAccessLevel(String accessName,MetaData metaData) throws InvalidAccessException
{
AccessManagerHandler handler = null;
try {
String packge=accessManagerPackageMap.get("Bhavik");
System.out.println("Package Name="+packge);
Constructor c = Class.forName(packge+accessName).getConstructor(MetaData.class);
handler = (AccessManagerHandler)c.newInstance(metaData);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
System.out.println("No such handle found...."+accessName);
throw new InvalidAccessException();
}
System.out.println(handler.getClass().getName());
logger.info("ClassOFWIKI="+handler.getClass().getName());
return handler;
}
}
现在我编写了测试用例,它将使用 JMockit 模拟getAccessLevel(String s1,MetaData mData)
。但我收到java.lang.NoSuchMethodException: com.os.bhavik.Bhavik.<init>(com.os.dbo.MetaData)
的错误。我该如何解决?我哪里错了?
这是使用JMockit的My TestCase
@RunWith(JMockit.class)
public class AbstractAccessManagerTest1
{
@Mocked
LogService logService;
MetaData metaData;
@Mocked
Bhavik bvk;
@Mocked
Constructor constructor;
HashMap<String, String>map = new HashMap<String,String>();
HashMap<String, String>map1 = new HashMap<String,String>();
AbstractAccessManager ab;
String accessName = "Bhavik";
@Test
public void testMathod() throws InvalidAccessException,Exception
{
ab = new AbstractAccessManager();
map.put("Bhavik", "com.os.bhavik.");
map1 =Deencapsulation.invoke(ab, "fillMap");
String name =Deencapsulation.invoke(map1, "get", accessName);
assertEquals("com.os.bhavik.", name);
new Expectations()
{
{
Class.forName(name+accessName).getConstructor(MetaData.class);
returns(constructor);
}
};
assertEquals(map,map1);
}