title: testing with Mockito java.lang.nullpointerexception
body: <p>i'm testing my web-app with junit and mockito (for business layer).</p>
  
  <p>i have an entity:</p>
  
  <pre><code>@Entity
  @Table(name = "brand")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "Brand.findAll", query = "SELECT b FROM Brand b"),
      @NamedQuery(name = "Brand.findByOid", query = "SELECT b FROM Brand b WHERE b.oid = :oid"),
      @NamedQuery(name = "Brand.findByName", query = "SELECT b FROM Brand b WHERE b.name = :name")})
  public class Brand implements Serializable {
  ...
  </code></pre>
  
  <p>and a session bean</p>
  
  <pre><code>@Stateless
  public class BrandFacade extends AbstractFacade&lt;Brand&gt; {
  
      @PersistenceContext(unitName = "MyWheelJSFPU")
      private EntityManager em;
  
      public void setEm(EntityManager em) {
          this.em = em;
      }
  
      ...
      @Override
      public List&lt;Brand&gt; findAll(){
          return em.createNamedQuery("Brand.findAll", Brand.class).getResultList();
      }
      ...
  </code></pre>
  
  <p>now i want to test .findAll</p>
  
  <pre><code>public class BrandFacadeTest {
  
      @Mock
      private EntityManager mockedEntityManager;
      @Mock
      private TypedQuery mockedQuery;
      private BrandFacade brandFacade;
  
      public BrandFacadeTest() {
      }
  
      @BeforeClass
      public static void setUpClass() {
          System.out.println("Start BrandFacadeTest");
      }
  
      @AfterClass
      public static void tearDownClass() {
      }
  
      @Before
      public void initDependencies() {
          brandFacade=new BrandFacade();
          brandFacade.setEm(mockedEntityManager);
          System.out.println("mockEmCreated");
      }
  
      /**
       * Test of findAll method, of class BrandFacade.
       */
      @Test
      public void testFindAll() {
          System.out.println("findAll");
          List&lt;Brand&gt; brands=new  ArrayList&lt;&gt;();
  
          when(mockedEntityManager.createNamedQuery("Brand.findAll", Brand.class)).thenReturn(mockedQuery);
          when(mockedQuery.getResultList()).thenReturn(brands);
          int initNumber=brandFacade.findAll().size();
  
          //creating Brand
          Brand b1 = new Brand(1, "mo");
          brandFacade.create(b1);
          verify(mockedEntityManager, times(1)).persist(any());
  
          brands.add(b1);
          assertEquals("error",initNumber+1, brandFacade.findAll().size());
      }
      ...
  </code></pre>
  
  <p>at this line</p>
  
  <pre><code>when(mockedEntityManager.createNamedQuery("Brand.findAll", Brand.class)).thenReturn(mockedQuery);
  </code></pre>
  
  <p>it returns a java.lang.nullpointerexception...why??? what's the problem?</p>
  
  <p>i made exactly the same for another entity and it work! </p>