False positive on java:S5783 rule (multiple invocations throwing the same exception)

Hello everyone,

I’m facing a java:S5783 issue on a test code like this :

    @Test
	void testGetOffersWhenException() {
		try {
            // Configuring mocks
			SystemFault systemFault = new SystemFault();
			systemFault.setErrorCode("500");
			systemFault.setFaultDescription("Error");
            
            // can throw SystemException
            when(serviceMock.myMethod(any(myClass.class)))
                .thenThrow(new SystemException("", systemFault));
            when(propertiesMock.getEnv()).thenReturn("DEV");
            
            // Executing
            Request request = createRequest();
            // can throw commonsException
            myClass.getResponseElement(request);
            
            fail("Executing - Exception should be thrown");
		} catch (commonsException ex) {
			assertThat(ex).isNotNull();
		} catch (SystemException ex) {
			fail("Configuring - Exception should not be thrown");
		}
    }

In this case the two different exceptions are correctly catches by two different cases, but sonar detect it as “multiple invocations throwing the same checked exception”.

It recommands to refactor the code like this

    @Test
	void testGetOffersWhenException() {
		try {
            // Configuring mocks
			SystemFault systemFault = new SystemFault();
			systemFault.setErrorCode("500");
			systemFault.setFaultDescription("Error");
            
            // can throw SystemException
            when(serviceMock.myMethod(any(myClass.class)))
                .thenThrow(new SystemException("", systemFault));
            when(propertiesMock.getEnv()).thenReturn("DEV");
        } catch (SystemException ex) {
			fail("Configuring - Exception should not be thrown");
		}

        try {            
            // Executing
            Request request = createRequest();
            // can throw commonsException
            myClass.getResponseElement(request);
            
            fail("Executing - Exception should be thrown");
		} catch (commonsException ex) {
			assertThat(ex).isNotNull();
		} 
    }

What doesn’t look like a better solution.

Can you confirm you can reproduce it and can I consider this as a false positive or do I missed something ?

SonarQube used : SonarQube Cloud entreprise + SonarQube for IDE (IntelliJ V11.15.0.84329)
Language : Java
Java version : 21

Hi,

I’m wondering if your test files are properly being picked up as tests. Are lines of code measured for them? (You can see this on the Code tab, or see if you can drill into the package and see them in the Lines of Code Measure.) If you do see Lines of Code for these files, then it means that they’re not properly being identified as test files, which would explain why this rule - which I don’t think normally runs on tests - is being applied. If that’s the case, we can work on your analysis configuration to straighten this out.

 
Ann

Hi Ann, thanks for the reply,

I think the problem is not if test file should be scanned or not (event if yes, I’ll exclude it from the scan), but the detection of an issue where there is not. Do you think this is a poor detection of the mock mechanism ? So it would no be reproductible outside test files ?

Tristan

Hi Tristan,

I agree with you that an issue shouldn’t be raised here. But the root cause, IMO, is not that the rule doesn’t understand mocks and should be updated to add that understanding. It’s that it was never intended to run on tests, so there was no intention for it to understand mocks or anything else test-related. Does that make sense? Or am I missing something?

 
Thx,
Ann