Java 正在测试中的方法调用私有void方法,我也想将其包括在测试中

Java 正在测试中的方法调用私有void方法,我也想将其包括在测试中,java,exception,junit,junit4,Java,Exception,Junit,Junit4,我有一个JUnit,我想用它来测试异常。是这样的: @Test public void test1() throws Exception { boolean testPass; try { method1(); testPass = true; Assert.assertTrue(testPass); } catch(Exception e) { testPass = false; Assert.assertTrue(testPass);

我有一个JUnit,我想用它来测试异常。是这样的:

@Test
public void test1() throws Exception {
  boolean testPass;
  try {
    method1();
    testPass = true;
    Assert.assertTrue(testPass);
  }
  catch(Exception e) {
    testPass = false;
    Assert.assertTrue(testPass);
  }
  System.out.println("End of test2 Junit");
}
public void method1() throws Exception {
  try {
    do something....
    method2();
  } catch (Exception e) {
    throw e;
  } finally {
   do some more...
  }
}
private  void method2() throws Exception {
  if (confition is not met) {
    do something...
    throw new Exception();
  } else {
    do something else;
  }
}
方法1是这样的:

@Test
public void test1() throws Exception {
  boolean testPass;
  try {
    method1();
    testPass = true;
    Assert.assertTrue(testPass);
  }
  catch(Exception e) {
    testPass = false;
    Assert.assertTrue(testPass);
  }
  System.out.println("End of test2 Junit");
}
public void method1() throws Exception {
  try {
    do something....
    method2();
  } catch (Exception e) {
    throw e;
  } finally {
   do some more...
  }
}
private  void method2() throws Exception {
  if (confition is not met) {
    do something...
    throw new Exception();
  } else {
    do something else;
  }
}
对于我想要的,只要考虑方法1,我的测试就可以了。我的问题是method1调用method2,并且还可能引发异常。是这样的:

@Test
public void test1() throws Exception {
  boolean testPass;
  try {
    method1();
    testPass = true;
    Assert.assertTrue(testPass);
  }
  catch(Exception e) {
    testPass = false;
    Assert.assertTrue(testPass);
  }
  System.out.println("End of test2 Junit");
}
public void method1() throws Exception {
  try {
    do something....
    method2();
  } catch (Exception e) {
    throw e;
  } finally {
   do some more...
  }
}
private  void method2() throws Exception {
  if (confition is not met) {
    do something...
    throw new Exception();
  } else {
    do something else;
  }
}

有可能method1不会引发异常,但method2会引发异常。我希望我的测试检查其中一个的异常,但我不确定如何将method2考虑到我的测试中,特别是因为它是一个私有的void方法。这可以做到吗?如果可以,如何做到?

根据您的代码,只有在以下情况下才能达到真正的条件:

如果由于某些原因,您无法在单元测试中准备此类条件,例如,需要Internet连接,您可以将条件检查提取到新方法中:

  if (isNotCondition()) {
    do something...
    throw new Exception();
在测试类中,重写新方法并返回所需内容:

MyService myService = new MyService() {
    @Override
    boolean isNotCondition() {
        return true;
    }
}
这是测试异常情况的更简洁的方法:

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testMethod1WhenMethod2ThrowsException() throws Exception {
    thrown.expect(Exception.class);
    thrown.expectMessage("expected exception message");

    myServive.method1();
}

你在我的测试中使用方法2是什么意思?是否要将method2的逻辑与method1一起测试?如果可能,我只希望assertTrue在method1或method2中未引发异常时通过,在method1或method2引发异常时失败。在这种情况下,如果method2引发异常或其任何子类,您的测试将失败。您应该测试公共接口,而不是私有实现。单元测试应该是黑盒测试,因为测试关注于从用户角度发生的事情。因为用户不能调用method2,所以应该没有理由测试它。这些测试的存在是为了确保实现产生预期的结果。如果method2抛出method1接受的异常,那么用户首先不应该知道该异常。你为什么要测试它?你是否在method1的合同中泄露了method2的细节?如果是这样,那就是问题所在。啊,好的。谢谢你。