如何在 Java 测试中使用 System.lineSeparator() 作为常量

Ane*_*ndo 0 java testng

我正在尝试System.lineSeparator()在 Java testng 测试用例中使用来构建expectedExceptionsMessageRegExp如下。

@Test(description = "When path parameter has given unmatch data type in ballerina",
            expectedExceptions = BallerinaOpenApiException.class,
            expectedExceptionsMessageRegExp = "File has errors: " + System.lineSeparator() + "Semicolon is missing at line 45")
public void testMissionSemicolon() throws IOException, BallerinaOpenApiException {
  // test implementation
}
Run Code Online (Sandbox Code Playgroud)

我在 处遇到以下错误expectedExceptionsMessageRegExp

Attribute value must be constant
Run Code Online (Sandbox Code Playgroud)

出现这个错误的原因是什么?我怎样才能达到我的目的?

Bri*_*etz 9

编译时常量和运行时常量之间存在区别。注释值必须是编译时常量。

编译时常量是编译时已知的值,例如

static final int ZERO = 0;
Run Code Online (Sandbox Code Playgroud)

运行时常量是已知不会改变的值,但其值在编译时未知:

static final int RANDOM = random.nextInt();
Run Code Online (Sandbox Code Playgroud)

注释值必须是编译时常量。

由于值会lineSeparator()根据您运行的操作系统而变化,因此它不能是编译时常量。