2016-07-05 173 views
1

我试图在我的代码上运行单元测试,并且一次测试失败,当我更深入时我注意到Base64.decode()在测试环境中总是返回null。我的具体情况是:Base64解码在测试环境中返回null

的方法来测试

public String decode(String jwt){ 
    System.out.println(jwt); //eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9 
    byte[] data = Base64.decode(jwt, Base64.DEFAULT); 
    return new String(data, "UTF-8"); //null pointer exception - data = null 
} 

测试本身:

@Test 
public void validateBase64Decode() { 
    String stringToTest = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9"; 
    String expectedResult = "{\"sub\": \"1234567890\", \"name\": \"John Doe\", \"admin\": true}"; 
    Assert.assertEquals(Util.decode(stringToTest), expectedResult); 
} 

测试总是失败,因为空指针异常:

java.lang.NullPointerException 
    at java.lang.String.<init>(String.java:481) 

因此我去进一步发现测试环境中的任何字符串都无法解码:

@Test 
    public void validateBase64Decode() { 
     Assert.assertNotNull(Base64.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9", 0));  
} //Test NEVER passes - assertion error 

请问,这里的任何大师?哪里不对 ?在此先感谢

+0

当然,“anyString”不能被base64解码。因为它不是base64编码的。你应该给出一个更好的例子,失败。 – greenapps

+0

举一个例子,你从你编码的数据开始。然后解码并与原始数据进行比较。 – greenapps

+0

如果您想将其用于json Web令牌,则有一些库可以完成这项工作。 –

回答

0

与robotium其工作只是测试:

public void testValidateBase64Decode() throws InterruptedException { 
    assertTrue("EcoScoreTesterActivity Activity never started", solo.waitForActivity(MainMenuActivity.class, WAIT_FOR_ACTIVITY_TIMEOUT)); 
    String stringToTest = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9"; 
    String expectedResult = "{\"sub\": \"1234567890\", \"name\": \"John Doe\", \"admin\": true}"; 
    //The string decode is different as yours 
    expectedResult = "{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"admin\":true}"; 

    assertNotNull(Base64.decode(stringToTest, 0)); 
    String result = new String(Base64.decode(stringToTest, 0)); 
    //Show in logcat result of decoding 
    Log.i("Test-Decoding","result+":result); 
    assertTrue("Error decoding", result.equals(expectedResult)); 
} 

在你情况下,它应该是这样:

public void testvalidateBase64Decode() { 
    String stringToTest = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9"; 
    String expectedResult = "{\"sub\": \"1234567890\", \"name\": \"John Doe\", \"admin\": true}"; 
    //The string decode is different as yours 
    expectedResult = "{\"sub\":\"1234567890\",\"name\":\"John Doe\",\"admin\":true}"; 
    String result = new String(Base64.decode(stringToTest, 0)); 
    //Show in logcat result of decoding 
    Log.i("Test-Decoding","result+":result); 
    Assert.assertTrue("Error decoding", result.equals(expectedResult)); 
} 

我只是检查这两个选项,它的工作在我的工作区

+0

我没有使用robotium,你的测试在我的环境中在同样的事情上失败了。 ? –

+0

我使用PowerMockito –

+1

我猜测单元测试无法访问android.util.Base64类,因此返回null。请查看此链接:tools.android.com/tech-docs/unit-testing-support。我想你shouId写它作为仪器测试代替使用例如robotium –

0

加入编辑gradle组:'commons-codec',名称:'commons-codec',版本:'1.9' 并通过导入使用此方法

import org.apache.commons.codec.binary.Base64 
new String(Base64.encodeBase64(inData)) 
Base64.decodeBase64(inString.getBytes())