我的同事开心的好日子,
我正在尝试为我的VectorHelper类编写数据驱动单元测试。由于该类在PointF对象上工作很多,并且这些对象在主机JVM上的android.jar中没有实现,因此我使用Robolectric框架中的所谓阴影。它确实可以在一个简单的单元测试中工作,但是当我尝试将PointF对象放入参数化测试时,它不起作用并始终返回PointF,其中x = 0且y = 0。 就像使用存根构造函数一样,没有实际设置字段。这是我的代码:
import android.graphics.PointF;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Created by Greg Stein on 9/4/2016.
*/
@RunWith(ParameterizedRobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class VectorHelperHorizontalAlignmentUnitTest {
private static final float THRESHOLD = 0.1f;
private static final Logger logger = Logger.getLogger(VectorHelperVerticalAlignmentUnitTest.class.getName());
public enum AlignmentType {ALIGNMENT_TYPE_VERTICAL, ALIGNMENT_TYPE_HORIZONTAL};
private static AlignmentType mAlignmentType;
// Reference vector
private static final PointF u1 = new PointF(0, 0);
private static final PointF u2 = new PointF(0, 0);
// Alignee
private static final PointF v1 = new PointF(0, 0);
private static final PointF v2 = new PointF(0, 0);
public VectorHelperHorizontalAlignmentUnitTest(PointF u1, PointF u2, PointF v1, PointF v2, AlignmentType alignmentType) {
logger.info("U: (" + u1.x + ", " + u1.y + ") --> (" + u2.x + ", " + u2.y + ")");
logger.info("V: (" + v1.x + ", " + v1.y + ") --> (" + v2.x + ", " + v2.y + ")");
this.u1.set(u1);
this.u2.set(u2);
this.v1.set(v1);
this.v2.set(v2);
this.mAlignmentType = alignmentType;
}
@Before
public void setup() {
logger.setLevel(Level.FINE);
}
@ParameterizedRobolectricTestRunner.Parameters
public static Collection mAlignVectorsTestData() {
return Arrays.asList(new Object[][] {
{new PointF(0, 0), new PointF(9, 0), new PointF(0, 0), new PointF(9, 1), AlignmentType.ALIGNMENT_TYPE_HORIZONTAL },
{new PointF(0, 0), new PointF(10, 0), new PointF(0, 0), new PointF(10, -1), AlignmentType.ALIGNMENT_TYPE_HORIZONTAL },
{new PointF(0, 0), new PointF(-10, 0), new PointF(0, 0), new PointF(-10, 1), AlignmentType.ALIGNMENT_TYPE_HORIZONTAL },
{new PointF(0, 0), new PointF(-10, 0), new PointF(0, 0), new PointF(-10, -1), AlignmentType.ALIGNMENT_TYPE_HORIZONTAL },
{new PointF(0, 0), new PointF(0, 10), new PointF(0, 0), new PointF(1, 10), AlignmentType.ALIGNMENT_TYPE_VERTICAL },
{new PointF(0, 0), new PointF(0, 10), new PointF(0, 0), new PointF(-1, 10), AlignmentType.ALIGNMENT_TYPE_VERTICAL },
{new PointF(0, 0), new PointF(0, -10), new PointF(0, 0), new PointF(1, -10), AlignmentType.ALIGNMENT_TYPE_VERTICAL },
{new PointF(0, 0), new PointF(0, -10), new PointF(0, 0), new PointF(-1, -10), AlignmentType.ALIGNMENT_TYPE_VERTICAL },
});
}
@Test
public void alignVectorSharpAngleTest() {
VectorHelper.alignVector(u1, u2, v1, v2, THRESHOLD);
// Test that end vertex has been changed:
switch (mAlignmentType) {
case ALIGNMENT_TYPE_VERTICAL:
assertThat(v2.x, is(v1.x));
break;
case ALIGNMENT_TYPE_HORIZONTAL:
assertThat(v2.y, is(v1.y));
break;
}
}
}
日志输出为:
2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:U:(0.0,0.0) - > (0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.VectorHelperHorizontalAlignmentUnitTest 信息:V:(0.0,0.0) - > (0.0,0.0)
如何解决?
提前谢谢你,格雷格。