为接口编写junit测试的最佳方法是什么,以便它们可用于具体的实现类?
例如,你有这个接口和实现类:
public interface MyInterface {
/** Return the given value. */
public boolean myMethod(boolean retVal);
}
public class MyClass1 implements MyInterface {
public boolean myMethod(boolean retVal) {
return retVal;
}
}
public class MyClass2 implements MyInterface {
public boolean myMethod(boolean retVal) {
return retVal;
}
}
Run Code Online (Sandbox Code Playgroud)
您如何针对界面编写测试,以便将其用于课程?
可能性1:
public abstract class MyInterfaceTest {
public abstract MyInterface createInstance();
@Test
public final void testMyMethod_True() {
MyInterface instance = createInstance();
assertTrue(instance.myMethod(true));
}
@Test
public final void testMyMethod_False() {
MyInterface instance = createInstance(); …Run Code Online (Sandbox Code Playgroud) 当我为我的android应用程序编写日志包装器时,我注意到了一个奇怪的机器人Log.isLoggable方法.执行以下代码:
final String TAG = "Test";
Log.v(TAG, "verbose is active: " + Log.isLoggable(TAG, Log.VERBOSE));
Log.d(TAG, "debug is active: " + Log.isLoggable(TAG, Log.DEBUG));
Log.i(TAG, "info is active: " + Log.isLoggable(TAG, Log.INFO));
Log.w(TAG, "warn is active: " + Log.isLoggable(TAG, Log.WARN));
Log.e(TAG, "error is active: " + Log.isLoggable(TAG, Log.ERROR));
Run Code Online (Sandbox Code Playgroud)
生成以下LogCat输出:
VERBOSE/Test(598): verbose is active: false
DEBUG/Test(598): debug is active: false
INFO/Test(598): info is active: true
WARN/Test(598): warn is active: true
ERROR/Test(598): error is active: true
Run Code Online (Sandbox Code Playgroud)
虽然我使用详细和调试日志记录生成这些输出,但为什么我会得到详细信息并且调试未激活?
当我尝试使用sax在套接字上解析xml时,我遇到了一个奇怪的现象.经过分析,我注意到DataOutputStream在我的数据前添加了2个字节.
DataOutputStream发送的消息:
0020 50 18 00 20 0f df 00 00 00 9d 3c 3f 78 6d 6c 20 P.. .... ..<?xml
0030 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 3f 3e 3c version= "1.0"?><
0040 63 6f 6d 70 61 6e 79 3e 3c 73 74 61 66 66 3e 3c company> <staff><
0050 66 69 72 73 74 6e 61 6d 65 3e 79 6f 6e 67 3c 2f firstnam e>yong</
0060 …Run Code Online (Sandbox Code Playgroud)