SpotBugs: How to suppress RV_RETURN_VALUE_IGNORED on lambda unit test
Asked Answered
W

1

0

I have created a DTO class for EmailAddress as follows and I'm trying to write a basic unit-test for it

EmailAddress.java

package com.company.service.interfaces.types.contacts;

import org.apache.commons.lang3.Validate;
import org.immutables.value.Value;

/**
 * Type for wrapping a valid email addresslayer
 */
@Value.Immutable(builder = false)
public abstract class EmailAddress {

    @Value.Parameter
    public abstract String value();

    @Value.Check
    void validate() {
        Validate.notBlank(value(), "'emailAddress' cannot be blank"); // i want to unit-test this
    }
}

EmailAddressTest.java

package com.company.service.interfaces.types.contacts;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

class EmailAddressTest {

    @Test
    public void testBuilder_nullInput() {
        assertThatThrownBy(() -> ImmutableEmailAddress.of(null))     // spot-bugs is failing on this line
            .isExactlyInstanceOf(NullPointerException.class);
    }
}

The above test-case is failing in SpotBugs report with RV_RETURN_VALUE_IGNORED error as follows

Bug type RV_RETURN_VALUE_IGNORED (click for details)
In class com.company.service.interfaces.types.contacts.EmailAddressTest
In method com.company.service.interfaces.types.contacts.EmailAddressTest.lambda$testBuilder_nullInput$0()
Called method com.company.service.interfaces.types.contacts.ImmutableEmailAddress.of(String)
At EmailAddressTest.java:[line 11]

As I understand the suppression annotations such as @CheckReturnValue and @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED") only work on methods with bodies and not on lambdas.

How can I work around this?

Wigley answered 31/8, 2023 at 17:45 Comment(0)
W
0

I ended up adding a SpotBugs filter exclusion to suppress the false errors.

build.gradle

..
<!-- configs for spotbugs gradle plugin activation -->
..
..

spotbugs {
  ignoreFailures.set(false)
  excludeFilter = file('spotbugs-exclude.xml')
}

spotbugs-exclude.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Docs at http://findbugs.sourceforge.net/manual/filter.html -->
<FindBugsFilter>

    <Match>
        <!-- To suppress false warnings in unit-tests for lambdas not using return values. -->
        <Package name="~com\.company\.service\.interfaces\.types\.contacts"/>
        <Bug pattern="RV_RETURN_VALUE_IGNORED"/>
    </Match>

</FindBugsFilter>

Wigley answered 31/8, 2023 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.