2

私はcatchブロックを持っています。catchブロックを実行したいです。私のクラスファイルは、

    public class TranscoderBean implements TranscoderLocal {
    public byte[] encode(final Collection<?> entitySet) throws TranscoderException {
        Validate.notNull(entitySet, "The entitySet can not be null.");
        LOGGER.info("Encoding entities.");
        LOGGER.debug("entities '{}'.", entitySet);

        // Encode the Collection
        MappedEncoderStream encoderStream = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            // Create the encoder and write the the DSE Logbook messgae
            encoderStream = new MappedEncoderStream(outputStream, this.encoderVersion);
            encoderStream.writeObjects(new ArrayList<Object>(entitySet), false);
            encoderStream.flush();
        }
        catch (Exception e) {
            LOGGER.error("Exception while encoding entities", e);
            throw new TranscoderException("Failed to encode entities", e);
        }
        finally {
            if (encoderStream != null) {
                try {
                    encoderStream.close();
                }
                catch (IOException e) {
                    LOGGER.error("Exception while closing the endcoder stream.", e);
                    throw new TranscoderException("Failed to close encoder stream", e);
                }
            }
        }
     }

私のテストクラスファイルは、

public class TranscoderBeanTest {

    private TranscoderBean fixture;

    @Mock
    MappedEncoderStream mappedEncoderStream;
    @Test
    public void encodeTest() throws TranscoderException {
        List<Object> entitySet = new ArrayList<Object>();
        FlightLog log1 = new FlightLog();
        log1.setId("F5678");
        log1.setAssetId("22");

        FlightLog log2 = new FlightLog();
        log2.setId("F5679");
        log2.setAssetId("23");
        entitySet.add(log1);
        entitySet.add(log2);

        MockitoAnnotations.initMocks(this);
        try {
            Mockito.doThrow(new IOException()).when(this.mappedEncoderStream).close();

            Mockito.doReturn(new IOException()).when(this.mappedEncoderStream).close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] encode = this.fixture.encode(entitySet);
        Assert.assertNotNull(encode);
    } 
}

Mockito.doThrow および Mockito.doReturn メソッドを試しましたが、まだ catch ブロックは実行されません。何が間違っていますか。

4

3 に答える 3

0

あなたは正しいテストクラスを持っていますか?あなたのTranscoderBeanへの参照はありません

于 2015-04-02T06:52:56.073 に答える
0

あなたは、Mockitoが主張していないことをすることを期待しています:

Mockito.doThrow(new IOException()).when(this.mappedEncoderStream).close();

このステートメントは、誰かがclose()mapperEncoderStream-Object を呼び出すたびにIOException. を呼び出すことはありませんclose

Mockito アクションの後に追加しようとするmapperEncoderStream.close();と、catch ブロックが入力されますが、mockito はここでは役に立たないため、これは問題の解決にはなりません。

問題については、次の代替案を検討できます。

リライト

encoderStream = new MappedEncoderStream(outputStream, this.encoderVersion);

encoderStream = createMappedEncoderStream(outputStream);

MappedEncoderStream createMappedEncoderStream(ByteArrayOutputStream outputStream) {
  return new MappedEncoderStream(outputStream, this.encoderVersion);
}

これにより、モックを依存関係として注入できます。

次に、次のようにフィクスチャを初期化します。

fixture = new TranscoderBean() {
  MappedEncoderStream createMappedEncoderStream(ByteArrayOutputStream outputStream) {
    return mappedEncoderStream; //this is your mock
  }
}

これにより、モックが TranscoderBean.encode メソッドに挿入されます。

次に、モック注釈を変更します。

@Mock(answer=CALLS_REAL_METHODS)
MappedEncoderStream mappedEncoderStream;

これが必要なのは、encode メソッドがcloseonだけでなくandmappedEncoderStreamも呼び出すためです。これらの呼び出しは例外をスローする可能性があるため、モックするか、実際のオブジェクトへの呼び出しに置き換える必要があります。writeObjectsflush

そのようにテストを剪定します

@Test(expected=TranscoderException.class)
public void encodeTest() throws TranscoderException {
    //... same as above
    MockitoAnnotations.initMocks(this);
    Mockito.doThrow(new IOException()).when(this.mappedEncoderStream).close();

     this.fixture.encode(entitySet); //this will throw an exception
}

これにより、次のことが行われます。

  • エンコード メソッドが返されませんnull! をスローするTranscoderExceptionので、次のように配置されますexpected
  • close例外スローでメソッドをオーバーライドする
  • エンコードを呼び出す
于 2015-04-06T14:03:12.277 に答える