小编Der*_*k K的帖子

Android上的Mockito,Context.getString(id)和NullPointerException

我刚刚开始学习Mockito测试框架,我遵循了以下官方教程:developer.android.com

代码是:

private static final String FAKE_STRING = "HELLO WORLD";

@Mock
Context mMockContext;

@Test
public void readStringFromContext_LocalizedString() {
    // Given a mocked Context injected into the object under test...
    when(mMockContext.getString(R.string.hello_world))
            .thenReturn(FAKE_STRING);
    ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext);

    // ...when the string is returned from the object under test...
    String result = myObjectUnderTest.getHelloWorldString();

    // ...then the result should be the expected one.
    assertThat(result, is(FAKE_STRING));
}
Run Code Online (Sandbox Code Playgroud)

我写了以下ClassUnderTest:

public class ClassUnderTest {

private Context context;

public ClassUnderTest(Context context)
{
    this.context=context;
}

public String …
Run Code Online (Sandbox Code Playgroud)

testing android unit-testing mockito

10
推荐指数
2
解决办法
7047
查看次数

Android“?colorPrimary”与“?attr/colorPrimary”?

我找不到任何关于以下差异的信息:

android:textColor="?attr/colorPrimary"
Run Code Online (Sandbox Code Playgroud)

对比

android:textColor="?colorPrimary"
Run Code Online (Sandbox Code Playgroud)

我读过“?attr”表示当前主题中指定的属性值,但没有“attr”它会给出相同的结果(=我的主题中定义的颜色)。它的行为与其他属性相似吗?

例如:

是否android:background="?attr/selectableItemBackground" 等于android:background="?selectableItemBackground"

据说这里有所不同。

非常感谢。

android android-styles android-attributes

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

Gradle:如何使用特定版本的快照?

我正在使用 graphhopper 库的 SNAPSHOT 版本。

是否可以使用旧版本的快照?我试过:

 compile(group: 'com.graphhopper', name: 'graphhopper', version: '0.6-20151126.110118-54') {  

    exclude group: 'com.google.protobuf', module: 'protobuf-java'
    exclude group: 'org.openstreetmap.osmosis', module: 'osmosis-osm-binary'
    exclude group: 'org.apache.xmlgraphics', module: 'xmlgraphics-commons'
}
Run Code Online (Sandbox Code Playgroud)

作为存储库我使用:maven { url "https://oss.sonatype.org/content/groups/public/" }

我也尝试过这个存储库:

maven {url "https://oss.sonatype.org/content/repositories/snapshots/"}
Run Code Online (Sandbox Code Playgroud)

但也找不到。

Could not find com.graphhopper:graphhopper:0.6-20151126.110118-54.
 Searched in the following locations:
     https://jcenter.bintray.com/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.pom
     https://jcenter.bintray.com/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.jar
     https://oss.sonatype.org/content/groups/public/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.pom
     https://oss.sonatype.org/content/groups/public/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.jar
     file:/D:/adt-bundle-windows-x86/sdk/extras/android/m2repository/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.pom
     file:/D:/adt-bundle-windows-x86/sdk/extras/android/m2repository/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.jar
     file:/D:/adt-bundle-windows-x86/sdk/extras/google/m2repository/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.pom
     file:/D:/adt-bundle-windows-x86/sdk/extras/google/m2repository/com/graphhopper/graphhopper/0.6-20151126.110118-54/graphhopper-0.6-20151126.110118-54.jar
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用 gradle 缓存的本地路径(在删除最新的快照目录并保留我想使用的版本之后),但没有成功。如果不可能,有什么办法可以停止下载新的 SNAPSNOT 版本吗?

感谢帮助。

android gradle graphhopper

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

清洁架构和授权。正确的方法?

我正在使用Clean Architecture模式使用Android应用程序,并且对如何以干净的方式实现授权感到怀疑。就干净的体系结构而言,以下解决方案是否干净?

我将创建以下用例(从表示层执行):

  • LoginUseCase (对于提供的登录名和密码,可通过远程服务获取api令牌并保存在本地令牌源中)
  • LogoutUseCase(从中清除令牌LocalTokenSource

LocalTokenSource接口将存储在域层中,其实现将存储在数据层中-一种存储库)

并且为了在每次应用启动时执行令牌刷新(从用户角度来看这不是用例,对吗?),我将SessionManager在域层中创建组件。SessionManager将负责刷新令牌并将其保存在中LocalTokenSource。每次活动开始时,我都会从其主持人处执行refreshToken()注入SessionManager.操作。您如何看待解决方案?

如果很干净,那么如何处理将令牌传递到远程服务以执行其他需要令牌的API方法?可以说我有PostsRepository从远程服务中获取帖子数据的信息。我应该将令牌从用例传递到类似的存储库方法repo.getPosts(token)吗?还是注入LocalTokenSource存储库,以便它可以自己读取令牌?第二个选项不会因为LocalTokenSource使用2层而违反了Clean Architecture规则?

authentication android use-case clean-architecture

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