简体   繁体   中英

How to quickly mock services in spring unit tests?

@Bean
public EntityManager(EntityManagerFactory emf) {
   //...
}

How can I quickly mock both of these beans (em + emf) inside a JUnit test? Is there some framework that allows me to define eg @Mock EntitiyManager em; ?

Take a look at springockito together with spring-test . It integrates spring with mockito and supports both annotation based mocks and mocks configured in a spring applicationContext.

You seem to be aware of the @Mock annotation, so presumably you know what Mockito is. You just mock the EntityManager exactly the same way you would mock anything else.

@Mock EntitiyManager em;
initMocks();
MyService myService = new MyServiceImpl(em);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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