How to notify JUnit of exceptions thrown in @DataPoints annotated methods?

I applied a general test for methods hashCodeand equalsusing the JUnit experimental annotation @Theory. The test case class itself is based on the dfa version .

However, when I tried to test the class java.net.InetAddress, I ran into a particular problem if the method providing the data contains code that throws an exception (in this case UnknownHostException):

So, I tried two alternatives that led to the same unsatisfactory result:

  • Declare the method as the appropriate exception:

    @DataPoints
    public static InetAddress[] declareException() throws UnknownHostException {
        return new InetAddress[] {
            InetAddress.getByName("not a valid internet address")
        };
    }
    
  • Explicitly catch the exception and re-throw as AssertionError:

    @DataPoints
    public static InetAddress[] rethrowAsAssertionError() {
        try {
            return new InetAddress[] {
                InetAddress.getByName("not a valid internet address")
            };
        } catch(UnknownHostException ex) {
            throw new AssertionError(ex);
        }
    }
    

a AssertionError " , . : []", @DataPoints.

- , JUnit (, , ) JUnit?

+5
2

137: DataPoints.

- @BeforeClass, DataPoints:

private static InetAddress[] datapoints;

@BeforeClass
public static void generateData() throws UnknownHostException {
  // do all the work of generating the datapoints
  datapoints = new InetAddress[] {
    InetAddress.getByName("not a valid internet address")
  };
}

@DataPoints
public static InetAddress[] data() {
  return datapoints;
}

.

328: @, DataPoints, , .

+8

. . . DataPoints. , .

enter image description here

0

All Articles