0

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

2 回答 2

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()是抛出异常。看看源代码

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

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

于 2013-05-08T08:24:32.487 回答
0

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

切换自

@RunWith(MockitoJUnitRunner.class)

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

解决了这个问题。

于 2017-06-28T19:00:39.390 回答