2017-09-18 36 views
0

我试图编写使用JPA作为DAO层的Create(Post)方法的单元测试。对于Mockito而言,我是新手,因此需要提供洞察。如何使用mockito模拟Post方法的JPA

1.EmployeeService .java 
@Component("IEmployeeService ") 
public class EmployeeService implements IInputService { 
@Inject 
EntityManagerFactory emf; 

@PersistenceContext 
EntityManager em; 

public InputEntity create(InputEntity inputEntity) { 
      em = emf.createEntityManager(); 
    try { 
     em.getTransaction().begin(); 
     inputEntity.setLST_UPDTD_TS(new Date()); 
     inputEntity.setLST_UPDTD_USER_ID(new String("USER1")); 
     em.persist(inputEntity); 
     em.getTransaction().commit(); 
    } catch (PersistenceException e) 

    { 
     if (em.getTransaction().isActive()) { 
      em.getTransaction().rollback(); 
     } 
     throw new WebApplicationException(e,Response.Status.INTERNAL_SERVER_ERROR); 
    } 

    finally { 
     em.close(); 
    } 
    return inputEntity; 
} 

2.InputEntity.java是实体类的getter和setter相应列的员工年龄,工资等。

现在,如果Post方法被调用,EmployeeService类中的create方法将被调用。我必须使用mockito编写单元测试,并且获取空指针,下面是我写的测试。

@Category(UnitTest.class) 
@RunWith(MockitoJUnitRunner.class) 
public class EmployeeServiceTest { 
    @Before 
    public void initMocks() { 
     MockitoAnnotations.initMocks(this); 
    } 

    @Autowired 
    EmployeeService employeeService; 

    @Mock 
    InputEntity inputEntity; 

    @Mock 
    EntityManagerFactory emf; 

    @Mock 
    private EntityManager em; 

    @Mock 
    private EntityTransaction et; 

    @Rule 
    public ExpectedException expectedException = ExpectedException.none(); 

    @Test 
    public void test_create_employee_success() throws Exception { 

    InputEntity expected = Mockito.mock(InputEntity .class); 
    Mockito.when(em.getTransaction()).thenReturn(et); 
    Mockito.when(emf.createEntityManager()).thenReturn(em); 
    Mockito.doReturn(expected).when(employeeService).create(inputEntityMock); 
    InputEntity actual = new InputEntity();  
    Mockito.doReturn(actual).when(employeeService).create(inputFileRoleValidationMock); 
    assertEquals(expected, actual); 

} 

回答

0

你有一个本地的模拟expected,您正试图在一个的assertEquals()使用,但永远不会工作,因为assertEquals将使用Object.equals,并劫持的Mockito为equals内部使用。一个Mockito模拟将永远不会Object.equals除了本身。

0

我们有类似的NullPointerException异常问题,这是因为实体管理器。在我们更新了setUp方法中的连接属性之后,我们可以调用JPA。

你可以尝试设置这样的连接属性。

//declare emfactory 
    private static EntityManagerFactory emfactory; 
    private static EntityManager em; 

    @BeforeClass 
    public static void setUp() throws Exception{ 
      Map<Object, Object> properties = new HashMap<Object, Object>(); 
      properties.put("openjpa.ConnectionURL", 
        "jdbc:db2://yourserver:port/DBName"); 
      properties.put("openjpa.ConnectionUserName", "username"); 
      properties.put("openjpa.ConnectionPassword", "userpassword"); 
      //set Schema 

      //set Driver Name 

      emfactory = Persistence.createEntityManagerFactory("PersistenceUnitName", 
           properties); 
      em = emfactory.createEntityManager(); 
      Mockito.when(emf.createEntityManager()).thenReturn(em); 
    } 

此外,您还需要修改test_create_employee_success()以使其工作。你用这种方法嘲笑一切,你不应该这样做。你可以尝试这样的事情。

@Test 
    public void test_create_employee_success() throws Exception { 
    { 
     InputEntity inputEntity = new InputEntity(); 
     employeeService.create(inputEntity); 
    } 
+0

欣赏使用您的见解.I'm实际上JPA春天开机,因此在创建设置类的实体管理器还是给了我一个问题。因此,我尝试用@asg模拟DataSource,好像现在一样,从长远来看,我必须尝试测试真实的源代码。 – DinaMike

0

需要在代码以下变化:

@Autowired 
EmployeeService employeeService; 

1.Instead可以使用:

@InjectMocks 
private EmployeeService EmployeeService; 
  • 另外,当你在方法中嘲笑 - InputEntity expected = Mockito.mock(InputEntity .class);除非在一些其他方法中使用,否则您不需要在此类声明中进行。

  • 你也可以摆脱宣言 -

    InputEntity实际=新InputEntity();

    您不能使用new关键字来声明模拟对象和对象的相等性。

  • 清洁单元测试看起来像这样 -

    import javax.persistence.EntityManager; 
    import javax.persistence.EntityManagerFactory; 
    import javax.persistence.EntityTransaction; 
    
    import org.junit.Test; 
    import org.junit.runner.RunWith; 
    import org.mockito.InjectMocks; 
    import org.mockito.Mock; 
    import org.mockito.Mockito; 
    import org.mockito.runners.MockitoJUnitRunner; 
    
    @RunWith(MockitoJUnitRunner.class) 
    public class EmployeeServiceImplTest { 
    
        @Mock 
        private EntityManager em; 
    
        @Mock 
        private EntityManagerFactory emf; 
    
        @InjectMocks 
        private EmployeeService EmployeeService; 
    
        @Mock 
        private EntityTransaction et; 
    
        @Test 
        public void testCreate() throws Exception { 
         InputEntity expected = Mockito.mock(InputEntity.class); 
         Mockito.when(em.getTransaction()).thenReturn(et); 
         Mockito.when(emf.createEntityManager()).thenReturn(em); 
         EmployeeService.create(expected); 
         Mockito.verify(em, Mockito.times(1)).persist(expected); 
        } 
    
    } 
    
    +0

    它的工作原理。赞赏你的见解,它的干净简单,让我理解了这个概念 – DinaMike

    相关问题