小编Ram*_*mps的帖子

kotlin和ArgumentCaptor - IllegalStateException

我通过ArgumentCaptor捕获Class参数时遇到问题.我的测试类看起来像这样:

@RunWith(RobolectricGradleTestRunner::class)
@Config(sdk = intArrayOf(21), constants = BuildConfig::class)
class MyViewModelTest {
    @Mock
    lateinit var activityHandlerMock: IActivityHandler;

    @Captor
    lateinit var classCaptor: ArgumentCaptor<Class<BaseActivity>>

    @Captor
    lateinit var booleanCaptor: ArgumentCaptor<Boolean>

    private var objectUnderTest: MyViewModel? = null

    @Before
    fun setUp() {
        initMocks(this)
        ...
        objectUnderTest = MyViewModel(...)
    }

    @Test
    fun thatNavigatesToAddListScreenOnAddClicked(){
        //given

        //when
        objectUnderTest?.addNewList()

        //then
        verify(activityHandlerMock).navigateTo(classCaptor.capture(), booleanCaptor.capture())
        var clazz = classCaptor.value
        assertNotNull(clazz);
        assertFalse(booleanCaptor.value);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试,下面抛出异常:
java.lang.IllegalStateException:classCaptor.capture()不能为null
是否可以使用参数的俘虏在科特林?

=========更新1:
Kotlin:1.0.0-beta-4584
Mockito:1.10.19
Robolectric:3.0

=========更新2:
Stacktrace:

java.lang.IllegalStateException: classCaptor.capture() must not be null

at com.example.view.model.ShoplistsViewModelTest.thatNavigatesToAddListScreenOnAddClicked(ShoplistsViewModelTest.kt:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at …
Run Code Online (Sandbox Code Playgroud)

android robolectric kotlin

37
推荐指数
5
解决办法
1万
查看次数

适用于Android的附近连接API - 不适用于某些设备

我正在使用此处提供的示例应用程序测试Nearby connection API:https://github.com/googlesamples/android-nearby 似乎这对某些设备不起作用.我成功地将三星Galaxy S3与Nexus 7连接在一起(S3作为主机,N7作为从机,反之亦然).但是,当我尝试将Samusung Galaxy S3连接到Nexus 5时,连接总是失败,状态代码为8005.

您可以在下面看到slave(发现设备)调用的方法,以便连接到主机(广告设备).

private void connectTo(String endpointId, final String endpointName) {
    debugLog("connectTo:" + endpointId + ":" + endpointName);

    // Send a connection request to a remote endpoint. By passing 'null' for the name,
    // the Nearby Connections API will construct a default name based on device model
    // such as 'LGE Nexus 5'.
    String myName = null;
    byte[] myPayload = null;
    Nearby.Connections.sendConnectionRequest(mGoogleApiClient, myName, endpointId, myPayload,
            new Connections.ConnectionResponseCallback() {
                @Override
                public …
Run Code Online (Sandbox Code Playgroud)

android google-nearby

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

Android的仪器测试 - 如何在方向更改后接收新的活动?


我正在尝试测试,如果新创建的Activity(在方向更改后)正确地重新初始化.下面的代码显示从getActivity()返回的活动是在setUp()中构造的活动,而不是新创建的活动.

测试:

public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity>{
 private static final String TAG = "RAMPS";
 private MyActivity mActivity;

 public MyActivityTest() {
    super("com.ramps", MyActivity.class);       
 }

 protected void setUp() throws Exception {
     super.setUp();
     mActivity = getActivity();
     Log.v(TAG, "setUp; activity=" + mActivity);
 }

public void testOrienationChange(){
     mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     getInstrumentation().waitForIdleSync();
     MyActivity newActivity = getActivity(); //should be new, but it's not
     Log.v(TAG, "testOrienationChange; activity=" + newActivity);       
 }
}
Run Code Online (Sandbox Code Playgroud)


Activiy:

public class MyActivity extends Activity {
    private static final String TAG = "RAMPS";  

 public void onCreate(Bundle savedInstanceState) { …
Run Code Online (Sandbox Code Playgroud)

instrumentation android unit-testing android-testing

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

在linux下构建来自jenkins的android项目 - 构建失败,找不到导入的build.xml

我正在尝试从Jenkins构建简单的android项目,但构建失败并输出消息(详细):

Building in workspace /var/lib/jenkins/jobs/MyProj/workspace
[MyProj] $ ant -file build.xml help -v
Apache Ant version 1.8.1 compiled on October 13 2010
Buildfile: /home/ramps/MyProj/build.xml
Detected Java version: 1.6 in: /usr/lib/jvm/java-6-openjdk/jre
Detected OS: Linux
parsing buildfile /home/ramps/MyProj/build.xml with URI = file:/home/ramps/MyProj/build.xml
Project base dir set to: /home/ramps/MyProj
parsing buildfile jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
 [property] Loading /home/ramps/MyProj/local.properties
 [property] Loading /home/ramps/MyProj/ant.properties
Importing file /home/ramps/MyProj/custom_rules.xml from /home/ramps/MyProj/build.xml
Cannot find /home/ramps/MyProj/custom_rules.xml imported from /home/ramps/MyProj/build.xml
Importing file /usr/share/android-sdk-linux/tools/ant/build.xml from /home/ramps/MyProj/build.xml

BUILD FAILED …
Run Code Online (Sandbox Code Playgroud)

ant android jenkins

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

自定义 AuthenticationFailureHandler 中的 Flash 属性

登录失败时,我想将用户重定向到错误页面并显示有意义的错误消息。是否可以添加将传递给后续请求的 Flash 属性?

下面显示的代码不起作用。RequestContextUtils.getOutputFlashMap()返回null

public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler
{
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException
    {
        FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
        if (outputFlashMap != null)
        {
            outputFlashMap.put("error", "Error message");
        }
        response.sendRedirect(request.getContextPath() + "/error");
    }
}
Run Code Online (Sandbox Code Playgroud)

authentication spring spring-security

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