# S2699 - False Positives for Java MockMvc + SonarCloud

**URL:** <https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769>\
**Category:** SonarQube Cloud\
**Tags:** sonarqube-cloud, java\
**Created:** [January 8, 2021, 11:16pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769 "2021-01-08T23:16:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![joepf](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/joepf/32/18261_2.png) [@joepf](https://community.sonarsource.com/u/joepf)\
**Post date:** [January 8, 2021, 11:16pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/1 "2021-01-08T23:16:51Z")

</div>

Platform: SonarCloud  
Language: Java

For our integration tests we’re running into a false positive that expects at least a single assertion when using MockMvc. `andExpect` works as an assertion, but is not treated like one.  
I found a similar issue, where the fix was to upgrade their version:

> [@S2699 Tests should include assertions false positive for andExpect](https://community.sonarsource.com/t/s2699-tests-should-include-assertions-false-positive-for-andexpect/7305):
>
> 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/\*\*")) …

I’m unsure how to go about fixing this for SonarCloud.

Code snippet:

```auto
  @Test
  @SuppressWarnings("squid:S2699")
  void register400() throws Exception {

    mockMvc.perform(post("/register")
        .content(...)
        .andExpect(status().is(400))
        .andExpect(jsonPath("$.errorMessage", is("TEST ERROR MESSAGE")));
  }

```

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [January 27, 2021, 11:54am UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/3 "2021-01-27T11:54:09Z")

</div>

Hi @joepf and welcome to the community!

I tried to reproduce your problem and I can’t. According to the implementation we do support MockMvc methods for this rule. In order to investigate this case further I need link to your project in SonarCloud or at least the whole file with this issue.

My guess is either you have misconfigured project and semantic is broken, or your assertions are in different package than analyser expects, so are not considered as assertions.

Regards,  
Margarita

---

<div class="post-metadata">

**Author:** ![Nazariy\_Dovhii\_Trach](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nazariy_dovhii_trach/32/20218_2.png) [@Nazariy\_Dovhii\_Trach](https://community.sonarsource.com/u/Nazariy_Dovhii_Trach)\
**Post date:** [March 12, 2021, 11:19am UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/5 "2021-03-12T11:19:26Z")

</div>

Hello! We have the same issue in our project.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/9/695ae7d99fc649172a2ecfc6d32d2613b25540dd.png)

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [March 25, 2021, 1:57pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/7 "2021-03-25T13:57:54Z")

</div>

Hi @Nazariy,

Thank you for your message and welcome to the community. As you can probably see, we were unable to reproduce such case because Java analyser do support MockMvc assertions.

In order to make help us investigate your case, we will highly appreciate if you can share the version of the product you’re using and the version of Java analyser there. Also it will be very helpful if you can tell us, which version of Spring you’re using to test with MockMvc (or any other library you’re using, if it’s not from Spring) and a version of JUnit.

Another possible explanation could be a broken configuration or semantics, so it will be nice to see your log and/or configuration and properties.

Kind regards,  
Margarita

---

<div class="post-metadata">

**Author:** ![lrozenblyum](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/lrozenblyum/32/11922_2.png) [@lrozenblyum](https://community.sonarsource.com/u/lrozenblyum)\
**Post date:** [March 30, 2021, 7:34am UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/9 "2021-03-30T07:34:53Z")

</div>

@Margarita_Nedzelska I also started sufferring from the issue (got multiple false positives related to the problem, during the last night SonarCloud analyzing).  
We are using Spring 5.3.3, JUnit4.

Example

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/6/66d39e4d106677971d2a00cfc9d647da38c00d08.png)

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [April 9, 2021, 1:36pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/11 "2021-04-09T13:36:33Z")

</div>

Hello @lrozenblyum,

Thank you for the message. Unfortunately, I cannot reproduce the original case.  
Could you please explain a little bit more what does it mean “during last night SonarCloud analysis”. Does it mean, the issue was not reported before on this case or there was no such code before?

Could you please also check there are no warnings on your project. There could be a missing semantic problem that leads to FPs. It is usually caused by misconfiguration.

Regatds,  
Margarita

---

<div class="post-metadata">

**Author:** ![lrozenblyum](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/lrozenblyum/32/11922_2.png) [@lrozenblyum](https://community.sonarsource.com/u/lrozenblyum)\
**Post date:** [April 9, 2021, 2:45pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/13 "2021-04-09T14:45:36Z")

</div>

Hello @Margarita_Nedzelska!  
By ‘during last night SonarCloud analysis’ I meant that the code is pretty old (since July **2019** ), and Sonar hasn’t been complaining about it before.

About warnings: SonarCloud doesn’t show any warnings about analyzing itelf on its web interface. Or do you mean to check logs of the analyzing in the CI?

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [April 9, 2021, 3:08pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/14 "2021-04-09T15:08:18Z")

</div>

Yes, I meant both warnings in the web interface and in logs.

---

<div class="post-metadata">

**Author:** ![rfajarachmad](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/rfajarachmad/32/6463_2.png) [@rfajarachmad](https://community.sonarsource.com/u/rfajarachmad)\
**Post date:** [August 16, 2021, 5:55am UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/15 "2021-08-16T05:55:02Z")

</div>

Hello!, I have the same issue in my project, do we have a workaround or resolution for this?

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/6/4/642ff5313b8262738016da7c913cb670bd0f755e.png)

---

<div class="post-metadata">

**Author:** ![joepf](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/joepf/32/18261_2.png) [@joepf](https://community.sonarsource.com/u/joepf)\
**Post date:** [August 16, 2021, 5:07pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/16 "2021-08-16T17:07:26Z")

</div>

Unfortunately, the only workaround we found was to suppress the warning.

```auto
@SuppressWarnings("squid:S2699")

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [August 23, 2021, 5:07pm UTC](https://community.sonarsource.com/t/s2699-false-positives-for-java-mockmvc-sonarcloud/36769/17 "2021-08-23T17:07:36Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
