帐户:Mockito测试帐户

时间:2018-03-15 07:52:50

标签: android unit-testing mockito powermockito

我正在使用Mockito编写单元测试,以获取返回Account对象的方法。

我正在创建一个新帐户,如下所示:

        Dim objNavGroup
        Dim objNavFolder
        Dim objNavMod
        Dim objSelectedCal = objAppointmentFolder.Items
        objNavMod = objAppointmentFolder.GetExplorer.NavigationPane.Modules.GetNavigationModule(1)
        For Each objNavGroup In objNavMod.NavigationGroups
            For Each objNavFolder In objNavGroup.NavigationFolders
                If objNavFolder.DisplayName = "TestCalendar" Then
                    objSelectedCal = objNavFolder
                    GoTo ExitFromCalLoop
                End If
            Next
        Next
ExitFromCalLoop:
 objAppointmentRecord = Redemption.RedemptionLoader.new_MAPIFolder.Items.Add(objSelectedCal)

代码没有崩溃但我在调试时总是遇到这个异常:

  

方法抛出' java.lang.RuntimeException'例外。无法评估android.accounts.Account.toString()

Private Account testAccount = new Account("name", "type"); testAccount.name始终是testAccount.type

有人可以告诉我,如果我做错了,或者是否有正确的方法来模拟它并获得初始化时定义的相同帐户名称和类型?

1 个答案:

答案 0 :(得分:0)

一位正在工作的同事认为我们必须对Account对象进行反思,因为其字段定义为 final ,所以你必须按照以下步骤进行:

Account account = new Account("MyAccount", "SomeType");
Field nameField = account.getClass().getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(account, "your value");

// Set whatever field you want to configure
Field typeField = account.getClass().getDeclaredField("type");
typeField.setAccessible(true);
typeField.set(account, "your value");