2016-07-05 37 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())