我寫的JUnit下面提到的方法異常ArrayOutOfBound同時運行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;
}
}
棧跟蹤是 – Programmer 2013-05-08 08:16:14
現在我嘗試了byte [] temp = new byte [10]; \t \t \t/* ByteArrayOutputStream baos = new ByteArrayOutputStream(); \t \t ObjectOutputStream oos = new ObjectOutputStream(baos); \t \t oos.writeObject(data1); \t \t */ \t \t \t //轉換成字符串的InputStream \t \t \t 字節\t [] S = Base64.encodeBase64(溫度); \t \t \t DecodeToObject。decode(new String(s)); – Programmer 2013-05-08 09:14:23