Android PointF constructor not working in JUnit test

前端 未结 2 1986
面向向阳花
面向向阳花 2021-01-18 04:46

I have just stumbled on this while trying to write a JUnit test. Admittedly this is my first unit test in JUnit, but I do find the behaviour very puzzling.

p         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 05:09

    See http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

    When you run unit tests, you are using a dummy version of the android jar. Typically you will see "Method ... not mocked."exceptions, but since you are directly accessing public fields, these are simply default values.

    Depending on your requirements, you could just use a fake: your own subclass extending PointF

        public static class FakePointF extends PointF {
            FakePointF(float x, float y) {
                this.x = x;
                this.y = y;
            }
        }
    

    but in a more complex test you'll probably end up having to mock a whole lot of other methods.

    The solution isnt pretty: you need to run instrumented tests against an emulator or device, or move to using something like Robolectric where the test runner will substitute 'shadows' for you.

    Also see this StackOverflow answer: android.graphics.Point: all methods are stubs.

提交回复
热议问题