小编luk*_*jar的帖子

Robolectric无法找到资源或清单文件

我已经创建了一个新的TestProject并在我的testMethod中添加了以下行:

Robolectric.getShadowApplication().getString(R.string.mystring);
Run Code Online (Sandbox Code Playgroud)

我的测试失败了

android.content.res.Resources$NotFoundException: unknown resource 2131558482
Run Code Online (Sandbox Code Playgroud)

控制台显示以下警告:

WARNING: No manifest file found at .\..\..\MyProject\AndroidManifest.xml.Falling back to the Android OS resources only.
To remove this warning, annotate your test class with @Config(manifest=Config.NONE).
WARNING: no system properties value for ro.build.date.utc
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml是否需要获取字符串资源?我尝试通过org.robolectric.Config.properties@Config添加Manifest,但警告仍然发生,我无法获取字符串资源.我确保清单的相对路径是正确的.我也试过更改JUnit运行配置,但这没有帮助.

android android-manifest robolectric

16
推荐指数
3
解决办法
1万
查看次数

MockWebServer和带回调的Retrofit

我想模拟MockWebServer的网络通信.不幸的改造回调永远不会被调用.我的代码:

    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
    server.play();

    RestAdapter restAdapter = new RestAdapter.Builder().setConverter(new MyGsonConverter(new Gson()))
            .setEndpoint(server.getUrl("/").toString()).build();

    restAdapter.create(SearchService.class).getCount(StringUtils.EMPTY,
            new Callback<CountContainer>() {

                @Override
                public void success(CountContainer countContainer, Response response) {
                    System.out.println("success");
                }

                @Override
                public void failure(RetrofitError error) {
                    System.out.println("error");
                }
            });

    server.shutdown();
Run Code Online (Sandbox Code Playgroud)

当我在没有回调的情况下使用改装时,它可以工作.

android retrofit mockwebserver

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

一个Activity和屏幕旋转中的两个SearchView

我在一个xml布局中有两个SearchView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <SearchView
        android:id="@+id/my_first_custom_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </SearchView>

   <SearchView
        android:id="@+id/my_second_custom_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/my_first_custom_view" >
   </SearchView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我通过setContentView()将此布局扩展到我的MainActivity.然后我为彼此调用方法setQuery().

在屏幕旋转之前一切正常.当我旋转屏幕时,每个searchView都有文本"World"而不是"Hello"和"World".

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SearchView firstSearchView = (SearchView)     findViewById(R.id.my_first_custom_view);
        SearchView secondSearchView = (SearchView) findViewById(R.id.my_second_custom_view);

        firstSearchView.setQuery("Hello!", false);
        secondSearchView.setQuery("World", false);
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释出了什么问题吗?

java android

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

使用 Robolectric 和 Dagger Hilt 启动片段时不会注入片段的属性

当使用RobolectricDagger Hilt启动片段时,后期初始化字段不会注入片段中。

我有以下测试:

@RunWith(RobolectricTestRunner::class)
@HiltAndroidTest
@Config(application = HiltTestApplication::class)
class SampleTest {

    @Test
    fun checkFragmentProperty() {
        launchFragmentInHiltContainer<TestFragment> {
            // nothing
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 TestFragment 看起来像这样:

@AndroidEntryPoint
class TestFragment : Fragment() {

    @Inject
    lateinit var testClass: TestClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        testClass.some()
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此测试时,它显示错误kotlin.UninitializedPropertyAccessException: lateinit property testClass has not been initialized

我使用官方文档launchFragmentInHiltContainer中的方法,但当我只使用.lunchFragmentInContainerandroidx.fragment.app.testing

当我使用模拟器而不是 Robolectric 时,字段会被正确注入。

我尝试添加:

hilt {
    enableTransformForLocalTests = true
}
Run Code Online (Sandbox Code Playgroud)

并从控制台运行测试,但测试仍然失败

android dagger dagger-hilt

6
推荐指数
0
解决办法
739
查看次数