简体   繁体   English

模拟持久性类返回IllegalArgumentException

[英]Mock persistance class returns IllegalArgumentException

When I try to mock class Event with mock(Event.class) I've got: 当我尝试使用mock(Event.class)模拟类Event ,我得到了:

java.lang.IllegalArgumentException: Object: Mock for Event, hashCode: 640142691 is not a known entity type.

This is Event class: 这是事件类:

@Entity
@Table(name = "event")
@XmlRootElement
public class Event implements Serializable {
    private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Basic(optional = false)
   @Column(unique = true, name = "id")
   private Integer eventId;

   @Basic(optional = false)
   @NotNull
   @Size(min=1, max=255)
   @Column(name = "title")
   private String eventTitle;

   @Basic(optional = false)
   @JoinColumn(name = "created_by", referencedColumnName = "id")
   @ManyToOne
   private User eventCreatedBy;

   @Basic(optional = false)
   @Column(name = "created")
   @Temporal(TemporalType.TIMESTAMP)
   private Date eventCreated;

   public Event() {
   }

   // getters and setters for all database fields

   @Override
   public int hashCode() {
       int hash = 0;
       hash += (eventId != null ? eventId.hashCode() : 0);
       return hash;
   }

   @Override
   public boolean equals(Object object) { 
       if (!(object instanceof Event)) {
           return false;
       }
       Event other = (Event) object;
       if ((this.eventId == null && other.eventId != null) || (this.eventId != null && !this.eventId.equals(other.eventId))) {
           return false;
       }
       return true;
   }

   @Override
   public String toString() {
       return "Event[ eventId=" + eventId + " ]";
   }

This is testing class: 这是测试类:

public class EventTest {

   private static final Integer EVENT_ID = 1;
   private static final String EVENT_TITLE = "title";

   private EntityManager em;
   private EntityTransaction et;
   private EventServiceImpl eventService;

   @Before
   public void setUp() throws Exception {
       em = Persistence.createEntityManagerFactory("test").createEntityManager();
       et = em.getTransaction();
       eventService = new EventServiceImpl();
       eventService.em = em;

       Event event = mock(Event.class);

       et.begin();
       em.persist(event);
   }

   @After
   public void tearDown() throws Exception {
       em.close();
   }

   @Test
   public void getList() {
       List list = eventService.getList();
       Event data = (Event) list.get(0);

       assertEquals(1, list.size());
       assertEquals(EVENT_TITLE, data.getEventTitle());
   }

   @Test
   public void getById() {
       Event data = eventService.get(EVENT_ID);
       assertEquals(EVENT_ID, data.getEventId());
   }
}

Anyone knows how to deal with that? 有人知道如何处理吗?

You are trying to persist a mock of Event instead of a real instance of it. 您正在尝试保留Event的模拟,而不是其真实实例。 This won't work because, like the exception says, the entity manager doesn't recognize the type of the mock as entity. 这是行不通的,因为,就像异常情况一样,实体管理器无法将模拟的类型识别为实体。

If you want to write an integration test, you should avoid mocking and use your real classes instead of mocks. 如果要编写集成测试,则应避免模拟,而应使用真实的类而不是模拟。 Mocks are more commonly used to make unit tests independent of other classes. 嘲笑更常用于使单元测试独立于其他类。

Event event = new Event(); // Entity manager will recognize this as entity
...

et.begin();
em.persist(event);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM