首页 > 解决方案 > Null object in android test

问题描述

I'm working on a test in android using Spock. (I've tried also to do this in Kotlin and Java JUnit tests.)

I'm trying to create Account to mock accountManager.getAccountsByType(any) >> [testAccount] method in AccountManager.

To create Account I'm using just:

new Account(testUserName, testAccountType)

In debug it looks like : Account object in debug

This is whole test code needed to reproduce it:

import android.accounts.Account
import spock.lang.Specification

class Test extends Specification {
    def accountManagerMock = Spy(AccountManager)
    def testUserName = "testUserName"

    def testAccountType = "testAccountType"

    final testAccount = new Account(testUserName, testAccountType)

    def "Test"() {
        when:
        def account = new Account(testUserName, testAccountType)
        accountManagerMock.getAccountsByType(testAccountType) >> [account]

        then:
        account != null
    }
}

In debug account is null like on attached screenshot. What should I do to correctly create an Account object in the Android test?

标签: androidkotlinjunitspock

解决方案


推荐阅读