我正在尝试测试在Android上的UnitTest中找不到类。
发生了什么:
transitive依赖项的android库,这些依赖项在主机应用程序中得到解决com.example.package。ClassNotFoundException。如果开发人员移除依赖项,则应引发异常。示例代码我想测试
try {
sNetworkResponseBuilderClass = OkHttpNetworkResponse.Builder.class;
} catch (Exception e){
// <<<< I want to test this case
new ClassNotFoundException("Unable to find OkHttpNetworkResponse.Builder.class").printStackTrace();
return null;
}使用的库: hamcrast,mockito,JUnit 4.
你知道怎么做吗?
发布于 2016-04-22 12:29:46
因此,对我来说,您需要做的第一件事是提取代码中可以抛出ClassNotFoundException的部分,以便能够轻松地模拟它,如下所示:
public Class<? extends NetworkResponseBuilder> getNetworkResponseBuilderClass()
throws ClassNotFoundException {
// Your logic here
}然后,可以使用Mockito.spy测试真正的工厂实例,以便重新定义getNetworkResponseBuilderClass()方法的行为,如下所示:
public void testFactoryIfNetworkResponseBuilderNotFound() {
Factory factory = spy(new Factory());
when(factory.getNetworkResponseBuilderClass()).thenThrow(
new ClassNotFoundException()
);
// The rest of your test here
}
public void testFactoryIfNetworkResponseBuilderFound() {
Factory factory = spy(new Factory());
when(factory.getNetworkResponseBuilderClass()).thenReturn(
OkHttpNetworkResponse.Builder.class
);
// The rest of your test here
}关于Mockito.spy的更多细节。
发布于 2016-04-22 09:12:01
不太确定我是否正确理解了您的问题,但是如果抛出异常,您可以向JUnit查询:
@Test(expected=ClassNotFoundException.class)
public void testClassNotFoundException() {
// a case where the exception gets thrown
}发布于 2016-04-29 06:30:32
OkHttpNetworkResponse.Builder可以如下所示:
package com.example.model;
public class OkHttpNetworkResponse {
public static class Builder {
}
}将创建任何对象的工厂类:可能如下所示:
package com.example.factory;
public class Factory {
public static Object getInstance(String className)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
Class clazz = Class.forName(className);
return clazz.newInstance();
}
}FactoryTest Class:将测试是否抛出ClassNotFoundException可能如下: N.B:请仔细检查评论。
package com.example.factory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class FactoryTest {
Factory factory;
@Test(expected=ClassNotFoundException.class)
public void test() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
ClassLoader loader = FactoryTest.class.getClassLoader();
String directory = loader.getResource(".").getPath() + "/com/example/model";
File dir = new File(directory);
//Checking directory already existed or not..
assertTrue("Directory:"+dir.getPath()+" not exist",dir.exists());
//Deleting directory
deleteDirectoryProgramatically(directory);
//Checking directory already deleted or not..
assertFalse("Directory:"+dir.getPath()+" still exist",dir.exists());
//Now getInstance Method will throw ClassNotFoundException because OkHttpNetworkResponse.Builder.class has been deleted programatically.
Factory.getInstance("OkHttpNetworkResponse.Builder.class");
}
private void deleteDirectoryProgramatically(String directory) {
File dir = new File(directory);
System.out.println(dir.getAbsolutePath());
String[] files = dir.list();
for (String f : files) {
File fl = new File(directory,f);
System.out.println(f+ " deleted?"+fl.delete());
}
System.out.println(dir+ " deleted?"+dir.delete());
}
}https://stackoverflow.com/questions/36714998
复制相似问题