Java可自动关闭测试

z9smfwbn  于 2022-10-29  发布在  Java
关注(0)|答案(1)|浏览(121)
public class CloseableResource implements AutoCloseable {
private static boolean _closed = false;
int _n;

  public CloseableResource(int n){
    }

    public void use() throws Exception{
        throw new Exception("Exception");
    }

    @Override
    public void close() throws Exception{
        _closed = true;
    }

    public static boolean isClosed() {
        System.out.println(_closed);
        return _closed;
    }

@Test
public void testAutoClose() {
   boolean failed = false;

   try (CloseableResource res = new CloseableResource(2)) {
      assertTrue(res != null);
      res.use();
   } catch (Exception e) {
      assertTrue(CloseableResource.isClosed());
      failed = true;
   }
   assertTrue(failed == true);

   failed = false;

   try (CloseableResource res = new CloseableResource(3)) {
      assertTrue(res != null);
      res.use();
   } catch (Exception e) {
      fail("this code should not be reached");
      failed = true;
   } finally {
      assertTrue(CloseableResource.isClosed());
   }
   assertTrue(failed == false);
}

我试图使测试工作,关闭方法似乎工作,但我不明白为什么测试不,它总是以失败结束(“这段代码不应该达到”)。我需要一些指导,请。

wsewodh2

wsewodh21#

private static boolean _closed = false;为什么是 static?将其设为static会在所有示例上将其设为true或false,而不是在特定示例上。我不确定这是否是所需的行为。当您创建新的CloseableResource时,新的资源将保持关闭状态。在构造函数中,您必须将 static_close设回false。这将使您的测试通过,但是它将导致糟糕实现。
这就是我所做的,使它“工作”与Java 8。与Java 8,没有办法,我知道真正测试您的资源的自动关闭属性。

public class CloseableResource implements AutoCloseable {
    private boolean _closed = false;
    int _n;

    public CloseableResource() {
        _n = 1;
    }

//  public CloseableResource(int n) {
//      _n = n;
//  }

    public void use() throws Exception {
        if (_closed) {
            throw new Exception("Attempting to use a closed resource");
        }
    }

    @Override
    public void close() throws Exception {
        if(_closed) {
            throw new Exception ("Attempting to close a closed resource");
        }
        _closed = true;
    }

    public boolean isClosed() {
        System.out.println(_closed);
        return _closed;
    }

    @Test
    public void testAutoClose() {

        CloseableResource res = new CloseableResource();
        try {
            assertTrue(res != null);
            res.use();
            res.close();
        } catch (Exception e) {
            fail("This code should not be reached");
        }

        try {
            assertTrue(res != null);
            res.use();
        } catch (Exception e) {
            assertTrue(e.getMessage().equals("Attempting to use a closed resource"));
        }

        try {
            res.close();
        } catch (Exception e) {
            assertTrue(e.getMessage().equals("Attempting to close a closed resource"));
        }
    }
}

但是,如果使用Java 9或更高版本,则可以在try之外声明资源(它将是有效的final);从而允许您有效地测试资源的auto-closeable属性。

@Test
public void testAutoClose() {
    CloseableResource res = new CloseableResource();
    try(res) {
        assertTrue(res != null);
        res.use();
    } catch (Exception e) {
        fail("This code should not be reached");
    }

    try {
        res.use();
    } catch (Exception e) {
        assertTrue(e.getMessage().equals("Attempting to use a closed resource"));
    }

    try {
        res.close();
    } catch (Exception e) {
        assertTrue(e.getMessage().equals("Attempting to close a closed resource"));
    }
}

这将允许您使用同一个对象在同一个测试中测试所有条件。

相关问题