java - 使用 EclEMMA 的 Catch block 的代码覆盖率

标签 java testing junit mockito eclemma

我有 catch block ,我想执行 catch block 。我的类(class)文件是,

    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 block 。哪里做错了。

最佳答案

要测试 try-catch block ,您可以使用 TestNG 方法,该方法包括使用以下注释 expectedExceptions 实现测试方法。 这个方法的代码,你必须实现它才能引发这个异常,所以 catch block 将被执行。

你可以看看http://testng.org/doc/documentation-main.html#annotations

关于java - 使用 EclEMMA 的 Catch block 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29406310/

相关文章:

java - 在 Eclipse 中启用 Java8 支持?

java - JUnit 5 Surefire Maven : how to run tests for a dynamic web module project?

android - 如何在android中测试处理SQLite数据库的方法?

selenium - Java Eclipse - 期望浏览器二进制位置

java - 执行 JpaTest 时找不到 @SpringBootConfiguration

java - hibernate注释和连接

java - 具有多线程的 REST Api,用于在 Spring Boot 中处理文件

java - 如何单元测试文件访问(Java)?

PhpStorm 测试 : Cannot select PHPUnit to run test

user-interface - GUI的自动化测试