小编pro*_*typ的帖子

如何使用 Robolectric 编写 Android 服务单元测试

在使用测试覆盖我的完整 Android 项目时,我试图找到一种方法来使用 Robolectric 测试我的服务。我需要测试的服务包含很多需要测试的逻辑。

经过一番研究,我从 robolectric找到了一个如何使用 shadowService 测试服务的代码示例,但我仍然不知道如何启动该服务并测试其功能。

首先,我想创建所有依赖项的模拟并将它们注入到服务中。然后对于每种方法,我想编写测试以及具有不同模拟依赖方法的服务。

我怎样才能做到这一点?测试 Android 服务时的最佳实践是什么?有人知道一些好的方法吗?

我的测试和服务到目前为止,但服务没有启动(调试器不会进入服务内的断点):

    @RunWith(RobolectricTestRunner.class)
    @Config(constants = BuildConfig.class , sdk = 23)
    public class MyServiceTest {
    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    Context context;
    @Mock
    ApiService apiService;
    @Mock
    ServiceManagerImpl serviceManager;

    @InjectMocks
    private MyService myService;

    private ShadowService shadowService;

    @Before
    public void setUp(){
        // explicit service creation from robolectric example
        this.updateService = Robolectric.setupService(UpdateService.class);

        this.shadowService = shadowOf(this.updateService);
        MockitoAnnotations.initMocks(this);
        when(this.apiService.fetchData()).thenReturn(42);
    }

    @Test
    public void testService(){
        this.updateService.startService(new Intent());
        // Test …
Run Code Online (Sandbox Code Playgroud)

java android unit-testing android-service robolectric

5
推荐指数
0
解决办法
1785
查看次数

android布局片段和正常布局

你的想法是编写一个活动,将谷歌地图api v2显示为一个片段,并在按钮上设置两个按钮,用于处理服务并在地图上生成标记.

问题是现在是否可以将地图片段放入定义和处理按钮的常规布局中,或者我必须将按钮放在另一个片段中?

目前,即使我通过android:height限制大小,地图也会覆盖整个屏幕

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/DarkGrey">


<fragment 
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="410dp"
class="com.google.android.gms.maps.SupportMapFragment" />



        <LinearLayout 
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom">
            <Button 
                android:id="@+id/buttonStopTracking"
                android:text="@string/btn_stop_tracking"
                android:layout_width="160dp"
                android:layout_height="60dp"
                android:textSize="20sp" 
                android:textStyle="bold" 
                />
            <Button
                android:id="@+id/buttonCreatePoI"
                android:text="@string/btn_create_PoI"
                android:layout_width="160dp"
                android:layout_height="60dp"    
                android:textSize="20sp"
                android:textStyle="bold" 
                />
        </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android google-maps fragment android-linearlayout

1
推荐指数
1
解决办法
5448
查看次数

Docker-compose 入口点脚本通过 exit 0 停止容器

执行入口点脚本后,容器以 Exit 0 停止。启动 Web 服务器的 compose 文件中指定的命令将被忽略。

我们使用 docker 和 docker-compose 作为 Rails 应用程序的环境。

入口点脚本:

#! /bin/bash
bundle exec rails assets:clobber
bundle exec rails assets:precompile

bundle exec rake db:exists && bundle exec rake db:migrate || bundle exec rake db:setup

rm -rf /aps/tmp/pids/server.pid

Run Code Online (Sandbox Code Playgroud)

撰写文件:

version: '2'
services:
  app:
    image: registry.gitlab.com/.../.../master:latest
    command: bundle exec rails server
    entrypoint: /aps/rails-entrypoint.sh
    volumes:
      - /srv/app/log/:/app/log
      - /srv/app/public/:/app/public
    env_file: .env
    ports:
      - '0.0.0.0:3333:3000'
    links:
      - apppostgres

  apppostgres:
    image: postgres
    ...


volumes:
  pgdata:
Run Code Online (Sandbox Code Playgroud)

当我在入口点脚本运行时连接到容器时,我可以看到执行的命令以ps auxas运行 …

ruby-on-rails docker docker-compose docker-entrypoint

1
推荐指数
1
解决办法
3569
查看次数