mardi 5 juillet 2016

Base64 decode returns null in test environment

I am trying to run Unit Tests on my code, and one test fails, when I got deeper I notice that Base64.decode() always return null in test environment. My specific case is:

The method to test

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 itself:

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

The test always fails due to null pointer exception:

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

Thus I went further and found that any String in test environment cannot be decoded:

 @Test
 public void validateBase64Decode() {
     Assert.assertNotNull(Base64.decode("anyString", 0));
 } //Test NEVER passes

Please, any guru here? What is wrong ? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire