admin管理员组

文章数量:1122832

I have a test that looks like this:

    @Test
    void postUploadShouldReturnOk() throws Exception {
        String body = prepareCorrectBody(true);

        myTestUtils.performRequestAndAssertStatus(body, status().isOk());
    }

I have a whole bunch of these tests in different test classes that all follow this pattern, with different bodies and expected statuses. That's why I extracted the performRequestAndAssertStatus to a utility class to avoid repetition. However, ever since I did that, SonarQube has marked this as an issue, saying "Tests should include assertions java:S2699"

Now, I've read the documentation for that rule, which can be found here: ;rule_key=java%3AS2699

According to that documentation, it should be possible to parameterize the rule so that it recognizes my custom assertion:

Furthermore, as new or custom assertion frameworks may be used, the rule can be parametrized to define specific methods that will also be considered as assertions. No issue will be raised when such methods are found in test cases. The parameter value should have the following format <FullyQualifiedClassName>#<MethodName>, where MethodName can end with the wildcard character. For constructors, the pattern should be <FullyQualifiedClassName>#<init>.

Example: company.CompareToTester#compare*,company.CustomAssert#customAssertMethod,company.CheckVerifier#.

Parameters

customAssertionMethods
Comma-separated list of fully qualified method symbols that should be considered as assertion methods. The wildcard character can be used at the end of the method name.

I have now tries to implement this by adding it to the existing and working sonar properties in my build.gradle like this:

sonar {
    properties {
        property "sonar.gradle.skipCompile", "true"
        property "sonar.projectKey", "my-service_12345678-1234-1234-1234-123412341234"
        property "sonar.projectName", "My-Service"
        property "sonar.coverage.exclusions", "**/MatomoClientImpl.java, **/ConverterClientImpl.java," 
        property "sonar.java.customAssertionMethods", "my_service.MyTestUtils#performRequestAndAssertStatus"
    }
}

However, that didn't work. I've tried all variations that I could think of too, but none of them worked.

Does anyone have any idea of how to get this to work? According to the documentation, it should be possible, right?

本文标签: