gpt4 book ai didi

android - 使用 Robolectric.buildAttributeSet() 进行单元测试会导致 NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 13:06:22 26 4
gpt4 key购买 nike

我有一个 PercentView 类,我正在尝试设置一个测试,以验证它是否被绘制为正确的大小。我已经为一个简单的栏工作了,但现在我想要一个图像/图标在它上面,这是一个可绘制的(实际上是矢量)。

一些注意事项:

  • 我有一个名为 app_enabled.xml 的可绘制文件(矢量),它是 main\res\drawable\app_enabled.xml
  • 我正在使用 Robolectric 3.5.1
  • 我在此处看到 Robolectric 团队的测试代码:Robolectric example: ViewStubTest.java

我已经声明了样式:

<declare-styleable name="PercentageView">
<attr name="image" format="integer" />
</declare-styleable>

和 View :

public class PercentageView extends View {
private int _activeColor;
private int _barBackgroundColor;
private float _barHeight;
private Drawable _icon;
private Paint _barBackgroundPaint;
private Paint _barActivePaint;
private int _percent;

public PercentageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray args = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PercentageView,
0,
0);

try {
_icon = args.getDrawable(R.styleable.PercentageView_image);

} finally {
args.recycle();
}

init();
}
}

现在是测试:

@RunWith(RobolectricTestRunner.class)
public class PercentageViewTests {
private ShadowCanvas _shadowCanvas;
private int _width;
private int _height;
private float _expectedPercentWidth;
private int _thePercent;
private Canvas _canvas;

private PercentageView SetUpView(int width, int height, int percent, boolean useIcon) {
ShadowApplication app = shadowOf(RuntimeEnvironment.application);

//Attempt to pass in the icon, if useIcon is true
AttributeSet attr = useIcon ? Robolectric.buildAttributeSet()
.addAttribute(R.styleable.PercentageView_image, "@drawable/app_enabled") //NullPointerException here
.build() : null;

PercentageView view = new PercentageView(app.getApplicationContext(), attr);
view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
view.setPercent(percent);

return view;
}

@Before
public void setup() {
Random random = new Random(10);
_width = random.nextInt(100);
random.setSeed(101);
_height = random.nextInt(200);

random = new Random(0);
_thePercent = random.nextInt(100);
_expectedPercentWidth = _width * _thePercent / 100;

_canvas = new Canvas();
_shadowCanvas = shadowOf(_canvas);
}

@Test
public void ShouldHaveIcon() {
PercentageView view = SetUpView(_width, _height, _thePercent, true);
view.onDraw(_canvas);

assertThat(_shadowCanvas.getRectPaintHistoryCount(), is(equalTo(3)));
}
}

还有非常可悲的堆栈跟踪:

java.lang.NullPointerException
at org.robolectric.Robolectric$AttributeSetBuilder.addAttribute(Robolectric.java:161)
at myorg.controls.PercentageViewTests.SetUpView(PercentageViewTests.java:41)
at myorg.controls.PercentageViewTests.ShouldHaveIcon(PercentageViewTests.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:523)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:62)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

最佳答案

尝试用 R.attr.image 替换 R.styleable.PercentageView_image

似乎 addAttribute() 的第一个参数需要是 attr res,而不是 stylable

关于android - 使用 Robolectric.buildAttributeSet() 进行单元测试会导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47480520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com