如何编写一个针对 Base64.decodeBase64 的 JUnit 测试。

huangapple go评论94阅读模式
英文:

How to write a JUnit test for Base64.decodeBase64

问题

以下是我想要编写 Junit 测试用例的方法

    public static byte[] getDecodeBase64(JSONObject object,String key) {
    		
      return Base64.decodeBase64(object.getString(key));
    }

我编写了如下测试

    @Test
    public void testGetDecodeBase64(){
    		
        JSONObject test = new JSONObject();
        test.put("clientId", "test");
        
        String value = "[B@[2807bdeb]";
        
        assertEquals(value, JSONUtil.getDecodeBase64(test, "clientId").toString());
        
    }

但每次该方法返回的值都不同
英文:

Below is my method for which i want to write Junit test case:

public static byte[] getDecodeBase64(JSONObject object,String key) {
		
  return Base64.decodeBase64(object.getString(key));
}

I wrote this:

@Test
	public void testGetDecodeBase64(){
		
		JSONObject test = new JSONObject();
		test.put("clientId", "test");
		
		String value = "[B@[2807bdeb]";
		
		assertEquals(value, JSONUtil.getDecodeBase64(test, "clientId").toString());
		
	}

But each time the value returned by the method is different.

答案1

得分: 1

数组的toString方法返回一个表示数组标识的字符串。

英文:

toString method of an array returns a string representing the Id of the array.

答案2

得分: 0

你应该使用 assertArrayEquals 来检查数组中的内容。

或者,如果你想要测试字符串,可以将字节数组转换为字符串:

byte[] bytes = {...}
String str = new String(bytes, "UTF-8");
英文:

You should use assertArrayEquals to check what's in the array.

Or, if you want to test with String, to convert your byte array into a String with :

byte[] bytes = {...}
String str = new String(bytes, "UTF-8");

huangapple
  • 本文由 发表于 2020年9月30日 20:14:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64137355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定