我将向我的方法发送一个实体作为它们的参数,这意味着我已经拥有要从数据库中插入、更新或删除的信息。我用我的持久性单元名称注入了实体管理器,所以我知道它有效,我的实体管理器的名称是“em”。我使用数据库连接从我的数据库中映射了我的类,我正在处理的类被命名为“TipoUsuario”这些是我的方法:
public void insert(TipoUsuario tipoUsuario) throws Exception {
if (em != null) {
em.persist(tipoUsuario);
}
}
public void update(TipoUsuario tipoUsuario) throws Exception {
if (em != null) {
em.merge(tipoUsuario);
}
}
public void delete(TipoUsuario tipoUsuario) throws Exception {
if (em != null) {
em.remove(tipoUsuario);
}
}
我正在研究第一种方法(插入),但我不知道如何测试我的方法......这是我测试插入方法的方法:
@Test
public void testInsert() throws Exception {
System.out.println("insert");
TipoUsuario tipoUsuario = new TipoUsuario(1, "Mantenedor", "AC2354", true);
//Instance of my class where I have my insert, update and delete methods
Utilidades instance = new Utilidades();
//I mock an entity manager with annotation @Mock and I pass that mocked entitytmanager to my the entitymanager that I have in my main class
instance.em = this.em;
//and that's all i got.. I don't know how to test if it really works
//i send my entity to my methor insert
instance.insert(tipoUsuario);
//i dont know what is return o how to use the assertEquals in this case...
assertEquals( ?, ?);
}
我嘲笑了实体管理器,因为那不是我代码的一部分,我知道其他人之前已经测试过,我唯一想测试的是我的方法是否将信息插入到数据库中。
波斯汪
慕后森
相关分类