- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在测试类中,我想提供我自己的 assertEquals
重载,其中包含一些不依赖于 Object.equals
的特殊逻辑。不幸的是,这不起作用,因为一旦我在本地声明我的 assertEquals
方法,Java 就不再从 org.junit.Assert.*
中找到静态导入.
有解决办法吗? IE。有没有办法为静态导入的方法提供额外的重载? (相当明显的解决方案是以不同的方式命名该方法,但该解决方案不具有相同的审美吸引力。)
我的测试类文件看起来像这样:
package org.foo.bar;
import static org.junit.Assert.*;
import org.junit.Test;
public class BarTest {
private static void assertEquals(Bar expected, Bar other) {
// Some custom logic to test equality.
}
@Test
public void testGetFoo() throws Exception {
Bar a = new Bar();
assertEquals(42, a.getFoo()); // Error *
}
@Test
public void testCopyConstructor() throws Exception {
Bar a = new Bar();
// Fill a.
Bar b = new Bar(a);
assertEquals(a, b);
}
}
错误 *
是“类型 BarTest
中的方法 assertEquals(Bar, Bar)
不适用于参数 (整数,整数)
。”
最佳答案
这个答案分为两部分——一个关于编译错误,另一个关于 assertEquals() 的用法
问题是在两个不同的命名空间中有两个 assertEquals() 方法 - 一个存在于 org.junit.Assert 命名空间中,另一个存在于 org.foo.bar.BarTest 命名空间(当前命名空间)中。
由于 shadowing rules declared in the Java Language Specification 编译器报告错误. Assert.assertEquals() 的静态导入被 BarTest 类中声明的 assertEquals() 隐藏。
解决方法(总是在隐藏声明的情况下)是使用 FQN(完全限定名称)。如果您打算使用 JUnit 断言类的 assertEquals(...),请使用
org.junit.Assert.assertEquals(...)
当你需要使用你的声明时,只需使用
assertEquals(...)
仅在 BarTest 中,它被遮蔽。在所有其他只需要 Assert.assertEquals() 或 BarTest.asserEquals() 之一的类中,您可以导入 Assert 或 BarTest(我认为您不需要在其他地方导入 BarTest,但还是声明了)。
当没有阴影时,您可以简单地导入类或静态方法并在没有 FQN 的情况下使用它。
需要考虑的其他事项
Assert.assertEquals() 在内部使用参数类的 equals() 方法。在你的测试用例中声明一个 assertEquals() 违反了 DRY 原则,因为 equals() 类型的方法应该被一致地实现和使用——在源代码和单元测试中放置两个不同的实现势必会造成混淆。
最好的方法是在 Bar 上实现 equals(),然后在您的测试用例中使用 Assert.assertEquals()。如果您已经拥有,则不需要 BarTest.assertEquals()。 assertEquals() 的伪代码有点像下面这样
关于java - 重载静态导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1448723/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!