【发布时间】:2015-12-30 10:31:12
【问题描述】:
我有一堂课:
public class Unit {
private int id;
private String unit;
private String dimension;
private Float factor;
private Context ctx;
public Unit(Context context){ this.ctx = context; }
public Unit(String unit, String dimension, Float factor, Context context) {
super();
this.ctx = context;
setUnit(unit);
setDimension(dimension);
setFactor(factor);
}
public void setDimension(String d) {
String[] dimensions = ctx.getResources().getStringArray(R.array.dimensions_array);
if(!Arrays.asList(dimensions).contains(d)){
throw new IllegalArgumentException("Dimension is not one of the permittable dimension names");
}
this.dimension = d;
}
...
}
为了针对 strings.xml 中的字符串数组验证“字符串维度”,我需要调用 getResources(),为此我需要一个上下文(这是我在此类中有上下文的唯一原因。)
这在应用程序中运行良好,但现在我想为 Unit 类编写 JUnit4 测试并想调用 Unit(),例如:
public class UnitTest {
Unit unit;
@Before
public void init() throws Exception {
System.out.println("Setting up ...");
unit = new Unit("dm","length", (float) 0.1,some_context); // What context should I put here?
}
@Test
...
}
如何在 UnitTest 类中获取上下文?或者我应该以某种方式重写测试?
【问题讨论】:
标签: java android unit-testing junit4 android-context