- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在尝试编写 JUnit 测试时偶然发现了这一点。诚然,这是我在 JUnit 中的第一个单元测试,但我确实发现这种行为非常令人费解。
package com.example.dom.pointfbugrepro;
import android.graphics.PointF;
import org.junit.Test;
import static org.junit.Assert.*;
public class ExampleUnitTest {
@Test
public void pointf_isCorrect() throws Exception {
PointF foo = new PointF(5, 0);
assertEquals(5, foo.x, 0.0001f);
}
}
在全新的 Android 项目中运行此测试会导致断言失败:
java.lang.AssertionError:
Expected :5.0
Actual :0.0
我在调查这个问题时发现的一件事是直接分配给 PointF 实例的 x
字段确实有效。
那么这里有什么问题呢?为什么构造函数没有正确设置字段?我应该如何测试使用 PointF Android 类的类?
最佳答案
参见 http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-
当您运行单元测试时,您使用的是 android jar 的虚拟版本。通常您会看到“Method ... not mocked.”异常,但由于您直接访问公共(public)字段,因此这些只是默认值。
根据您的要求,您可以只使用假的:您自己的扩展 PointF 的子类
public static class FakePointF extends PointF {
FakePointF(float x, float y) {
this.x = x;
this.y = y;
}
}
但在更复杂的测试中,您可能最终不得不模拟大量其他方法。
解决方案并不完美:您需要针对模拟器或设备运行插桩测试,或者转而使用 Robolectric 之类的工具,其中测试运行器将替换“shadows”。 ' 给你。
另见 StackOverflow 答案:android.graphics.Point: all methods are stubs .
关于java - Android PointF 构造函数在 JUnit 测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010251/
我正在尝试在 Android 应用程序中为 Canvas View 实现滚动。 我初始化一个全局 private PointF backgroundPosition = new PointF(0, 0
我需要在我的 C# 程序中对一组点进行硬编码。 C 风格的初始化程序不起作用。 PointF[] points = new PointF{ /* what goes here? */ }; 它
我们需要用 PointF(贝塞尔曲线上位置的 t 参数)存储一条额外的信息。 由于这个数据不容易重新计算,所以我想在计算点时将其与 PointF 一起存储,以供其他例程使用。 我们有数百个 Point
我有一个 PointF 数组,我想使用它通过 Graphics.DrawCurve 方法绘制曲线。 为此,我现在需要 X 和 Y 的最大值和最小值,以便正确缩放位图图像框。 如何在 PointF 的数
编辑:谢谢大家,伙计们。我很感激你。 我正在开发一个在两个值之间绘制函数的程序。这是代码: public partial class Form2 : Form { public class E
谁能帮我创建一个算法、IComparer 或某种方法来对 PointF 的数组或列表进行排序?元素。可以说我得到了关注 PointF我数组中的元素: [0] {X = 50.0 Y = 0.0} [1
这个问题类似于the one here . 我们都知道什么PointF是,不是吗?这是数据结构: public struct PointF { public float X; public f
好吧,我将点保存到如下所示的文件中: PointF p = new PointF(109.1679, 162.4473); string lineInFile = p.ToString(); // i
我认为这是一个简单的问题,但我想不出来。 //verifica qual o ponto mais proximo do centroide_circulo PointF ponto_mais_pro
documentation 中的示例代码CGContext.AddLines 没有为我编译。 ctxt.AddLines (new PointF[] { new PointF (30, 30)
我一直在寻找这个,但找不到任何东西。我的想法是我有一个类似 (52.66, 60.11) 的 PointF,我想以此 PointF 为中心绘制一个空圆。我试图用 DrawEllipse 做,但它不关心
我遇到了一个奇怪的性能问题,如果能对我所遇到的行为做出解释,那就太好了。 我正在使用System.Drawing.Region.IsVisible(PointF)确定某个点是否在多边形内。这通常效果很
我创建了两个点,就像一条线。我想将其转换为矩形。我应该怎么做? 例如,这就是我画线的方式。但我希望它是一个 Rectangle private PointF start, end; p
我不确定这个问题是否太琐碎了,但是我需要使用 following方法 Graphics.DrawImage 的重载: public void DrawImage( Image image,
我在尝试编写 JUnit 测试时偶然发现了这一点。诚然,这是我在 JUnit 中的第一个单元测试,但我确实发现这种行为非常令人费解。 package com.example.dom.pointfbug
我有一个返回 Point2D.double 的 java 方法,Android 最接近它的类是 PointF。问题是 PointF 返回 float 而不是 double。有没有办法重写 PointF
System.Drawing.Point 和 System.Drawing.PointF 有什么区别。你能举个这两者之间的例子吗? 提前致谢。 最佳答案 Point 使用整数坐标(int 代表 X 和
如果我有一个包含 PointF 的列表,例如: [10.5, 5.5] [8.5, 4.5] [10.5, 6.5] [5.5, 10.5] [10.5, 3.5] 是否可以让 LINQ 首先找到 x
我正在尝试在 Xamarin Studio 中的 Xamarin.iOS 项目中享受新的 PCL 乐趣。我为我的项目创建了一个 PCL“核心”库并添加了 Splat ( v0.3.4 ) 通过 NuG
查看我创建的以下单元测试: import android.graphics.PointF import org.junit.Assert.assertEquals import org.junit.T
我是一名优秀的程序员,十分优秀!