问个JUNIT语法的问题

flying520520 2010-06-01 07:01:29
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.os.cts;

import android.content.Context;
import android.os.Vibrator;
import android.test.AndroidTestCase;
import android.util.Log;
import dalvik.annotation.TestTargets;
import dalvik.annotation.TestTargetNew;
import dalvik.annotation.TestLevel;
import dalvik.annotation.TestTargetClass;

@TestTargetClass(Vibrator.class)
public class VibratorTest extends AndroidTestCase {

private Vibrator mVibrator;

@Override
protected void setUp() throws Exception {
super.setUp();
mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
}

@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test cancel()",
method = "cancel",
args = {}
)
public void testVibratorCancel() {
try {
mVibrator.vibrate(1000);
} catch (Exception e) {
fail("testVibratorCancel failed!");
}
sleep();
try {
mVibrator.cancel();
} catch (Exception e) {
fail("testVibratorCancel failed!");
}
}

@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test vibrate",
method = "vibrate",
args = {long[].class, int.class}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test vibrate",
method = "vibrate",
args = {long.class}
)
})
public void testVibratePattern() {
long[] pattern = {100, 200, 400, 800, 1600};
try {
mVibrator.vibrate(pattern, 3);
} catch (Exception e) {
fail("vibrate failed!");
}
try {
mVibrator.vibrate(pattern, 10);
fail("Should throw ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException e) {
}
sleep();
}

@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test vibrator with multi thread.",
method = "vibrate",
args = {long.class}
)
public void testVibrateMultiThread() {
Log.d("*******VibratorTest", "MultiTreadTest");
new Thread(new Runnable() {
public void run() {
Log.d("*******VibratorTest", "Thread 1");
try {
mVibrator.vibrate(100);
} catch (Exception e) {
fail("MultiThread fail1");
}
}
}).start();
new Thread(new Runnable() {
public void run() {
Log.d("*******VibratorTest", "Thread 2");
try {
// This test only get two threads to run vibrator at the same time
// for a functional test,
// but it can not verify if the second thread get the precedence.
mVibrator.vibrate(1000);
} catch (Exception e) {
fail("MultiThread fail2");
}
}
}).start();
sleep();
}

private void sleep() {
try {
Thread.sleep(10000);
} catch (Exception e) {
}
}
}





问个低级的问题:下面这段是什么意思呀? @TestTargets是什么意思呀?
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test vibrate",
method = "vibrate",
args = {long[].class, int.class}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Test vibrate",
method = "vibrate",
args = {long.class}
)
})








...全文
178 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
flying520520 2010-06-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 landor2004 的回复:]

这不是junit,这是google 的Android SDK 中的一部分,下面是他的API说明

http://docs.generalwebtech.com/java/android-javadoc-1.1_r1/dalvik/annotation/TestTargets.html
http://docs.generalwebtech.com/java/android-javadoc-1……
[/Quote]

看了,但还是不能理解,这些东西后括号里面的东西有什么用呀。
不懂JAVA,请指教
flying520520 2010-06-02
  • 打赏
  • 举报
回复
谢谢

请顶
flying520520 2010-06-02
  • 打赏
  • 举报
回复
Java5之后就出现了Java Annotation(JSR 175),第一眼看上去很像是C/C++的预编译,或者条件编译。可以告诉编译器应该做些什么事情之类的,但是情况其实并非如此,这个东西还是很有些不同之处,从一个用户的角度看,大体上可以用来做三件事体:

1. 编译器注释。JSR175提供了几个默认的Annotation,包括@Deprecated、@Override和@SuppressWarnings

2.文档化支持。Sun说可以替代JavaDoc的一些用途,但是也并没有新增什么,而且和注释相比,Annotation有作用域的限制,即不可以放到代码的各个位置,支持:TYPE、FIELD、METHOD、PARAMETER、CONSTRUCTOR、LOCAL_VARIABLE、 ANNOTATION_TYPE、PACKAGE。当然这些位置适用于所有的Annotation。

3.运行时支持。Annotation可以混合代码和元代码,而且支持设定到底是否在字节码中包含Annotation。如果包含,则可以使用 Reflection激活这些Annotation。Sun给出了类似单元测试的例子。不过似乎和我无关,Junit已经干的很不错了。

感觉Annotation似乎是一个大而全的方案,但是反而无法获得更多的应用。作为编译器注释,显然没有ifdef能力那么强,作为文档化支持,Javadoc早就广泛应用,作为runtime支持,则Junit这类物件也很完善了。

XDoclet Team不过XDoclet看上去是有希望的项目,这玩意儿可以通过源代码中的注释标签生成多种多样的副产品,包括源代码、接口、XML、文档等等。本来是没有什么好处的,但是目前J2EE的复杂性导致的大量冗余工作却可以使用注释来消除,具有如下好处:
Landor2004 2010-06-01
  • 打赏
  • 举报
回复
这不是junit,这是google 的Android SDK 中的一部分,下面是他的API说明

http://docs.generalwebtech.com/java/android-javadoc-1.1_r1/dalvik/annotation/TestTargets.html
http://docs.generalwebtech.com/java/android-javadoc-1.1_r1/dalvik/annotation/TestTargetNew.html

50,547

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧