S2699 Tests should include assertions false positive for andExpect

Hi Sonar Team!

I’m experiencing a false positive with rule S2699. The rule description explicitly mentions support for Spring’s org.springframework.test.web.servlet.ResultActions.andExpect(), so I expect the following test to be compliant.
However this is not the case, I do get an error for this test.

Environment:
SonarQube 7.6
SonarJava 5.11

Example test:

// springframework MockMvc used
@Test
void myTest() throws Exception {
    	mockMvc.perform(MockMvcRequestBuilders.get("/api/**"))
    	.andExpect(status().isForbidden())
    		.andReturn();
}

I think this is a false-positive.

Cheers and thanks for the help,
Balázs

2 Likes

Hi,

I’m having the same issue.
Does someone have an idea to solve that ?

Now in our projects we have hundreds of those false positive that are making noise and hiding the other issues

Thanks,

Maxime

Hello,

I confirm andExpect is considered by S2699 as you can see in the code

I can’t reproduce your problem with this reproducer:

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class AssertionsInTestsCheckTest {

  @Autowired
  WebApplicationContext wac;

  private MockMvc mockMvc;

  @Before
  public void setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  }

  @Test
  public void testWithNoAssertion1() { // Noncompliant
  }
  
  @Test
  void myTest1() throws Exception { // Compliant
    mockMvc.perform(MockMvcRequestBuilders.get("/api/**"))
    .andExpect(status().isForbidden())
    .andReturn();
  }
}

… using: org.springframework:spring-test:4.0.5.RELEASE

Which version of spring-test are you using?
Can you share a reproducer?

Thanks

Hi @Maxime,

Any news on my questions?