darophi
darophi

Reputation: 540

Testng Expected error messages with ANTLR 4

I am trying to use a dataProvider, that provides the grammar for the parser and the expectederrormessage.

@Test(expectedExceptions = org.antlr.v4.runtime.misc.ParseCancellationException.class, expectedExceptionsMessageRegExp = ".*line .*", dataProvider = "antlrInput")
public void TestParsing(String input, String empty) throws ParseCancellationException {
    ProgramLexer lexer = new ProgramLexer(new ANTLRInputStream(input));
    lexer.removeErrorListeners();
    lexer.addErrorListener(DescriptiveErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ProgramParser parser = new ProgramParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(DescriptiveErrorListener.INSTANCE);
    ParseTree tree = parser.program();
}

@DataProvider
public Object[][] antlrInput() {
    return new Object[][] {
            {"int x", "line 1:5 missing ';' at '<EOF>'"},
            {"int x, d", "line 1:5 mismatched input ',' expecting ';'"},
    };
}

Where it says expectedExceptionsMessageRegExp = ".* line .* i want to be able to use the second part of the dataProvider's array.

As it can be seen in the dataProvider the thrown message will be different according to what kind of grammar i use as input, and that is what i want to test.

Upvotes: 0

Views: 208

Answers (1)

Mrunal Gosar
Mrunal Gosar

Reputation: 4683

You would need to use one of the Assert methods in TestNG to do that..you cannot change the attribute value at Annotation on the fly until and unless you wanted to use reflections to do that..but in my view that would be over killing a simple requirement which can achieved by just using Assertions.
EDIT1: Assert.assertThrows or Assert.expectThrows or simply by adding that code block in try { } catch(ParseCancellationException p) {} block and then in catch block capture exception message and assert that with Assert.assertEquals(actualErrorMessage,expectedErrorMessage)

Upvotes: 2

Related Questions