Android Base64在单元测试中编码和解码返回null

时间:2022-12-02 23:30:30

I am attempting to decode a Base64 encoded string in Android using the http://developer.android.com/reference/android/util/Base64.html class.

我正在尝试使用http://developer.android.com/reference/android/util/Base64.html类解码Android中一个Base64编码的字符串。

Both the encodeToString and decode methods are returning null, and I have no idea what's wrong, here's my code for the decode:

encodeToString和decode方法都返回null,我不知道是怎么回事,这是我的解码代码:

// Should decode to "GRC"
String friendlyNameBase64Encoded = "R1JD";

// This returns null
byte[] friendlyNameByteArray = Base64.decode(friendlyNameBase64Encoded, Base64.DEFAULT);

// Fails with NullPointerException
String friendlyName = new String(friendlyNameByteArray, "UTF-8");

I'm running Android API 23.1.0

我运行的是Android API 23.1.0

3 个解决方案

#1


12  

I had the same problem in my unit tests. I didn't realize the Base64 class I'm using is a part of the Android API, therefore

我在单元测试中也遇到了同样的问题。因此,我没有意识到我使用的Base64类是Android API的一部分

You can't use android.util.Base64 in a regular JUnit test, it must be an Instrumentation test.

你不能使用android.util。在常规的JUnit测试中,它必须是一个仪器测试。

However, if you really want it as a unit test, you could use the Apache Commons Base64 class instead. Include it in Gradle build:

但是,如果您真的希望它作为单元测试,您可以使用Apache Commons Base64类。将其包含在Gradle构建中:

// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'

And then slightly different usage,

但是用法稍有不同,

#2


0  

Follow up o android tutorials and unit test notes while you need just Unit tests without use of some android libs

跟进o android教程和单元测试说明,当你只需要单元测试而不使用android libs时

In your case you're depending on android.Base64. I had similar issue and moving test classes from src/test -> src/androidTest worked. Those tests are run on virtual machine or real android device. I didn't notice the diff at the first look.

在你的情况下,你取决于android.Base64。我有类似的问题和移动测试类从src/test -> src/androidTest工作。这些测试在虚拟机或真正的android设备上运行。我一眼就没注意到有什么差异。

#3


0  

You can use the Robolectric Runner

您可以使用robectric Runner

  1. Add the dependency in your build.gradle:

    在您的build.gradle中添加依赖项:

    testCompile 'org.robolectric:robolectric:X.X.X'
    
  2. Add this line in your testing class:

    在测试类中添加这一行:

    import org.junit.runner.RunWith;
    import org.robolectric.RobolectricTestRunner;
    
    @RunWith(RobolectricTestRunner.class)
    public class MyTestingClassTest {
        ...
    }
    

#1


12  

I had the same problem in my unit tests. I didn't realize the Base64 class I'm using is a part of the Android API, therefore

我在单元测试中也遇到了同样的问题。因此,我没有意识到我使用的Base64类是Android API的一部分

You can't use android.util.Base64 in a regular JUnit test, it must be an Instrumentation test.

你不能使用android.util。在常规的JUnit测试中,它必须是一个仪器测试。

However, if you really want it as a unit test, you could use the Apache Commons Base64 class instead. Include it in Gradle build:

但是,如果您真的希望它作为单元测试,您可以使用Apache Commons Base64类。将其包含在Gradle构建中:

// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'

And then slightly different usage,

但是用法稍有不同,

#2


0  

Follow up o android tutorials and unit test notes while you need just Unit tests without use of some android libs

跟进o android教程和单元测试说明,当你只需要单元测试而不使用android libs时

In your case you're depending on android.Base64. I had similar issue and moving test classes from src/test -> src/androidTest worked. Those tests are run on virtual machine or real android device. I didn't notice the diff at the first look.

在你的情况下,你取决于android.Base64。我有类似的问题和移动测试类从src/test -> src/androidTest工作。这些测试在虚拟机或真正的android设备上运行。我一眼就没注意到有什么差异。

#3


0  

You can use the Robolectric Runner

您可以使用robectric Runner

  1. Add the dependency in your build.gradle:

    在您的build.gradle中添加依赖项:

    testCompile 'org.robolectric:robolectric:X.X.X'
    
  2. Add this line in your testing class:

    在测试类中添加这一行:

    import org.junit.runner.RunWith;
    import org.robolectric.RobolectricTestRunner;
    
    @RunWith(RobolectricTestRunner.class)
    public class MyTestingClassTest {
        ...
    }