findbugs: Read of unwritten field on @autowired forunit test

11,598

Solution 1

Try deleteing the error (go to problems or marker view, right-click and select delete). Do a full rebuild and see if the error comes back.

When you change the checkstyle rules, it doesn't delete the errors which came from the old rule. So you need to delete them manually.

Solution 2

The problem is with the case of the xml tags. It should be something like:

<FindBugsFilter>
  <Match>
    <Class name="~.*Test"/>
    <Bug pattern="UWF_UNWRITTEN_FIELD" type="UWF_UNWRITTEN_FIELD"/>
  </Match>
</FindBugsFilter>
Share:
11,598
Chun ping Wang
Author by

Chun ping Wang

I am software engineer programming in Java. Beside programming, I like read Bible and do my best to follow the command of Christ. Which means i shut down work after 5:00 pm on Friday to Saturday 7:30 pm. My football team is Baltimore Ravens, baseball San Francisco Giant, Hockey La Kings and for basketball, i just bandwagon on a good team.

Updated on June 05, 2022

Comments

  • Chun ping Wang
    Chun ping Wang almost 2 years

    Hi I am getting findbugs error on my JUNIT test.

    Here is my test.

    public class MyTest {
        @Autowired
        private MyService  myService;
    
       @Test
       public void serviceShouldSayMine() {
             Assert.assertEquals(this.myService.getText(), "Mine");
       }
    }
    

    my find bugs filter.

    <findbugsfilter>
    <match>
      <class name="~.*Test"/>
      <or>
        <field name="~.*Dao"/>
        <field name="~.*Service"/>
        <field name="~.*TestUtils"/>
      </or>
      <bug pattern="UWF_UNWRITTEN_FIELD" type="UWF_UNWRITTEN_FIELD"/>
    </match>
    <match>
      <class name="~.*Test"/>
      <or>
        <field name="~.*Dao"/>
        <field name="~.*Service"/>
        <field name="~.*TestUtils"/>
      </or>
      <bug pattern="NP_UNWRITTEN_FIELD" type="NP_UNWRITTEN_FIELD"/>
    </match>
    </findbugsfilter>
    

    I added the filter to eclipse but I still get the error Read of unwritten field myService when i click on it , it says

    Pattern id: UWF_UNWRITTEN_FIELD, type: UwF, category: CORRECTNESS
    
    This field is never written.  All reads of it will return the default value. 
    Check for errors (should it have been initialized?), or remove it if it is useless.
    

    and also

    Pattern id: NP_UNWRITTEN_FIELD, type: NP, category: CORRECTNESS
    
    The program is dereferencing a field that does not seem to ever have a non-null  
    value written to it. Dereferencing this value will generate a null pointer exception.
    

    What is wrong with my filter,

    my goal is to filter out all classes

    a.) Ending with *Test or *TestCase

    b.) Method inside above class ending with *Service or *Dao