[英]Exception ArrayOutOfBound while running Junit
I am writing a Junit for the below mentioned method 我正在为下面提到的方法编写Junit
Can somebody suggest appropriate solution to this 有人可以为此建议合适的解决方案吗
After running the Junit i am getting below exception 运行Junit后,我遇到了异常
java.lang.ArrayIndexOutOfBoundsException: -84
at org.apache.commons.codec.binary.Base64.isBase64(Base64.java:137)
at org.apache.commons.codec.binary.Base64.isArrayByteBase64(Base64.java:163)
My test class 我的考试课
public class DecodeToObjectTest extends TestCase {
public void testDecode() {
try {
String data="data";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(data);
// convert String into InputStream
DecodeToObject.decode(new String(baos.toByteArray()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The method which i need to test 我需要测试的方法
public static
Object decode(
String input ) throws IOException,
ClassNotFoundException
{
Log log = LogFactory.getFactory().getInstance(
"com.avocent.cps.report.service" );
try
{
boolean c = Base64.isArrayByteBase64( input.getBytes() );
if(log.isDebugEnabled()){
log.debug( "DecodeToObject :Entering Helper Input: " + input ); }
byte[] data = Base64.decodeBase64( input.getBytes() );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
if(log.isDebugEnabled()){
log.debug( "DecodeToObject :Object Input stream ois: " + ois ); }
Object obj = null;
obj = ois.readObject();
if(log.isDebugEnabled()){
log.debug(
"DecodeToObject :Convering object to String: "
+ obj.toString() );
}
ois.close();
if(log.isDebugEnabled()){
log.debug(
"DecodeToObject :Decoded byte array to object successfully " );
}
return obj;
}
catch( ClassNotFoundException cnfe )
{
if(log.isErrorEnabled()){
log.error( "DecodeToObject : Error :" );
}
throw cnfe;
}
catch( IOException ioe )
{
if(log.isErrorEnabled()){
log.error( "DecodeToObject : Error :" );
}
throw ioe;
}
}
It seems the input isn't Base64
encoded. 看来输入不是
Base64
编码的。 Try this : 尝试这个 :
boolean c = Base64.isArrayByteBase64( input.getBytes() );
if(log.isDebugEnabled()){
log.debug( "DecodeToObject :Entering Helper Input: " + input ); }
if(c) {
byte[] data = Base64.decodeBase64( input.getBytes() );
// your code
}
But I suppose the isArrayByteBase64()
is throwing the exception . 但是我认为
isArrayByteBase64()
了异常。 Look at the source code of 看一下源代码
private static boolean isBase64(byte octect) {
if (octect == PAD) {
return true;
} else if (base64Alphabet[octect] == -1) {
return false;
} else {
return true;
}
}
The static byte array base64Alphabet
doesn't have element at index '-84' and hence ArrayIndexOutOfBoundsException
. 静态字节数组
base64Alphabet
在索引'-84'处没有元素,因此ArrayIndexOutOfBoundsException
。
Instead of this: 代替这个:
byte[] data = Base64.decodeBase64( input.getBytes() );
Try this: 尝试这个:
byte[] s = Base64.encodeBase64URLSafe(input.getBytes());
byte[] data = Base64.decodeBase64(s);
I ran into the same Exception. 我遇到了同样的异常。 The JUnit tests passed on my IDE, but failed on Maven build.
JUnit测试在我的IDE上通过,但在Maven构建上失败。
Switching from 从切换
@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
to 至
@RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@RunWith(PowerMockRunner.class)@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
fixed the issue. 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.