Android 点F的机器人分子参数化测试阵列

Android 点F的机器人分子参数化测试阵列,android,robolectric,parameterized-unit-test,Android,Robolectric,Parameterized Unit Test,各位开发人员,您好 我正试图为我的VectorHelper类编写一个数据驱动的单元测试。由于该类主要处理PointF对象,而这些对象在主机JVM上的android.jar中没有实现,因此我使用了所谓的Robolectric框架的阴影。它在一个简单的单元测试中确实有效,但是当我尝试将PointF对象放入参数化测试中时,它不起作用,并且总是返回x=0和y=0的PointF就像使用存根构造函数一样,没有实际设置字段。这是我的密码: import android.graphics.PointF; im

各位开发人员,您好

我正试图为我的VectorHelper类编写一个数据驱动的单元测试。由于该类主要处理PointF对象,而这些对象在主机JVM上的android.jar中没有实现,因此我使用了所谓的Robolectric框架的阴影。它在一个简单的单元测试中确实有效,但是当我尝试将PointF对象放入参数化测试中时,它不起作用,并且总是返回x=0和y=0的PointF就像使用存根构造函数一样,没有实际设置字段。这是我的密码:

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.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:U:(0.0,0.0)-->(0.0,0.0) 2016年9月4日下午4:19:23 com.example.neutrino.maze.vectorHelper水平校准单元测试 信息:V:(0.0,0.0)-->(0.0,0.0)

如何修复它


提前谢谢你,Greg。

顺便说一句,更改u1/u2/v1/v2的静态初始值设定项没有帮助。你能确认在非参数化测试中使用
点F
是否正常工作吗?2尤根·马丁诺夫:是的。绝对地这就是我的帖子的重点。看起来当我在参数化测试中使用PointF时,我使用的是修改后的android.jar中的PointF,而没有robolectric shadowing.BTW,更改u1/u2/v1/v2的静态初始值设定项没有帮助。你能确认在非参数化测试中使用
PointF
是否正常工作吗?2Eugen Martynov:是的。绝对地这就是我的帖子的重点。看起来,当我在参数化测试中使用PointF时,我使用的是修改后的android.jar中的PointF,没有机械分子阴影。