我正在为下面提到的方法编写一个 Junit
有人可以为此提出适当的解决方案吗
运行 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)
我的测试班
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();
}
}
}
我需要测试的方法
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;
}
}