- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
public class MyProperties {
private Map<String, Object> properties = new HashMap<String, Object>();
public void setProperty(String name, Object value) {
properties.put(name, value);
}
@SuppressWarnings("unchecked")
public <T> T getProperty(String name) {
return (T) properties.get(name);
}
}
@Test
public void test() {
MyProperties props1 = new MyProperties();
props1.setProperty("name", "John Smith");
MyProperties props2 = new MyProperties();
props2.setProperty("name", "John Smith");
assertEquals(props2.getProperty("name"), props1.getProperty("name"));
}
上述单元测试在我的机器上通过,但在我们的 jenkins 环境中失败,并出现以下错误:
java.lang.String cannot be cast to [Ljava.lang.Object;
当通过 Eclipse 和 ant(eclipse 4.4 luna、ant 1.9.6、jdk_8_u60、Windows 7 64 位)运行时,单元测试在我的机器上通过,但在我们的 jenkins 环境(ant 1.9.6 jdk_8_u60、Ubuntu 12.04.4)中失败.它也在其他几个环境中失败,并在其他几个环境中工作——没有明显的韵律或原因。
所以我有两个问题:
1)Why is Java choosing the overload
org.junit.Assert.assertEquals(Object[],Object[])
instead oforg.junit.Assert.assertEquals(String,String)
ororg.junit.Assert.assertEquals(Object,Object)
?2) and why is the unit test passing in some environments, and failing in others with the same versions of java, ant, and junit?
最佳答案
关于 2:
不同的行为很可能不是由运行时环境引起的,而是由用于编译类的编译器引起的。
Eclipse 有自己的 Java 编译器,而 Jenkins 使用 JDK 中的 javac。
似乎是 Eclipse 4.4。生成对 assertEquals
行的调用,这允许成功的测试运行,而 javac 生成对 org.junit.Assert.assertEquals(Object[],Object[])
的调用然后测试失败。
这在 Eclipse 4.5 中不起作用,它会在编译时提示调用已弃用的方法 Assert.assertEquals(Object[],Object[])
并且在运行时测试也会失败 eclipse 4.5。
您可以通过反编译 Eclipse 4.4 和 javac 生成的类(使用 (javap -c
) 并检查它们选择的 Assert 方法来检验假设。
关于 1:
Assert 有两个 assertEquals 方法,它们接受两个对象:
Assert.assertEquals(Object expected, Object actual);
Assert.assertEquals(Object[] expecteds, Object[] actuals);
根据JLS (15.12) 在编译方法调用时,编译器必须从所有适用的方法中选择最具体的方法。在示例中,这是 Assert.assertEquals(Object[] expecteds, Object[] actuals)
。
这可能会令人困惑,正因为如此,JUnit 可能决定弃用带有数组参数的方法。
关于java - 为什么 Java 类型推断选择了错误的重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465322/
当使用模板模板参数时,我如何推断或删除模板模板的模板类型? 考虑以下 SSCCE: #include #include #include using namespace std; templat
假设我有一些特质: trait A[T] { def foo: T } 一个扩展它的类: class B[T](t: T) extends A[T] { def foo = t } 以及父特征的子特征
一边玩-rectypes在某些时候选择 OCaml 我只是迷路了。 这个表达式几乎可以打字: # fun x -> x x;; - : ('a -> 'b as 'a) -> 'b = 但是这里 O
我正在编写一个类似 CRUD 的应用程序,并且通过主键进行大量查找(主键可以有不同的类型)。所以我定义了以下类型类: {-# LANGUAGE MultiParamTypeClasses #-} cl
我已经创建了关系 A 'is functional parent of' B并定义 'has functional parent'作为 'is functional parent of' 的倒数. '
给定一个使用 Kotlin 版本 1.3.61 和 JOOQ 版本 3.13.1 的系统,这样的方法会构建 union正常查询: val selectCommonPart = coalesce
考虑以下错误代码: fun x = if (null x) then 0 else (take 50 x) : (fun (drop 50 x)) 我注意到,我可以毫无问题地将它加载到
给定一个具有以下类型的函数 a: a::x -> Bool 和以下类型的另一个函数 b: b::Bool -> y 我正在尝试找出推断以下函数类型的步骤: c =\d -> d a b 有人可以帮助解
我正在尝试使用 Infer 工具来分析我的应用代码。我关注了these steps每次我尝试运行 infer -- gradle build 时,我都会收到以下错误: infer -- gradle
所以我制作了这个模板来定义内联仿函数: template struct AsFunctor { template std::invoke_result_t operator()(A
是否可以推断 CRTP 基类中模板化成员函数的返回类型? 虽然推断参数类型效果很好,但它因返回类型而失败。考虑以下示例。 #include template struct base { tem
使用 Series.interpolate 很容易在 Pandas.DataFrame 中插入值,如何进行外推? 例如,给定一个如图所示的 DataFrame,我们如何将它外推 14 个月到 2014
我想知道为什么这不起作用(缺少参数类型)? Seq(1,2,3).toSet.map(_ + 1) 但这确实: val foo = Seq(1,2,3).toSet foo.map(_ + 1)
我没有必要使用 SQLite3 shell 工具来维护一个小型数据库。我正在使用 -header -ascii标志,尽管据我所知,这适用于任何输出选择。我正在寻找一种方法来避免对返回的任何一个值的类型
我有以下组件 type PropTypes = { items: T[], header: (item: T) => React.Element, body: (item: T) => R
我想在 Eclipse/JSDT 中指定实例变量的类型,如下例所示: /** * @constructor */ function A() { /** @type Node */
我正在用 Python 编写一个方法,它看起来像这样: def rgb_to_grayscale(image): print(image.shape) pass 此处预期的类型是 nu
我有一个 my_values 数组,我正在尝试为其推断 true_values 数组中最接近、较小的值。使用下面的 find_nearest 函数并不能完成我想要的。我如何追加它以找到最近的、较小的值
在下面的代码中: template int b(int q, const std::array& types) { int r = q; for (int t : types)
在 Pandas DataFrame 中插入 NaN 单元非常容易: In [98]: df Out[98]: neg neu pos av
我是一名优秀的程序员,十分优秀!