运行Junit时出现异常ArrayOutOfBound

时间:2013-05-08 08:15:50

标签: java junit

我正在为下面提到的方法编写一个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;
        }
    }

3 个答案:

答案 0 :(得分:0)

输入似乎不是Base64编码的。试试这个:

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
}

但我认为isArrayByteBase64()正在抛出异常。 <{3}}

 private static boolean isBase64(byte octect) {
     if (octect == PAD) {
        return true;
    } else if (base64Alphabet[octect] == -1) {
        return false;
    } else {
        return true;
     }
 }

静态字节数组base64Alphabet在索引'-84'处没有元素,因此ArrayIndexOutOfBoundsException

答案 1 :(得分:0)

而不是:

  byte[] data = Base64.decodeBase64( input.getBytes() );

试试这个:

 byte[] s = Base64.encodeBase64URLSafe(input.getBytes());
 byte[] data = Base64.decodeBase64(s);

答案 2 :(得分:0)

我遇到了同样的异常。 JUnit测试在我的IDE上传递,但在Maven构建时失败。

切换
  

@RunWith(MockitoJUnitRunner.class)

  

@RunWith(PowerMockRunner.class)@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)

解决了这个问题。