gpt4 book ai didi

java - 正确配置以模拟 Hibernate 的 sessionFactory.getCurrentSession()

转载 作者:行者123 更新时间:2023-11-30 10:26:27 24 4
gpt4 key购买 nike

我有以下内容:

@Repository
@Transactional
@HibernateProfile
public class PersonaHibernateRepository implements PersonaRepository {

private static final Logger logger = LoggerFactory.getLogger(PersonaHibernateRepository.class.getSimpleName());

private final SessionFactory sessionFactory;

public PersonaHibernateRepository(SessionFactory sessionFactory){
logger.info("{} constructor", PersonaHibernateRepository.class.getSimpleName());
this.sessionFactory = sessionFactory;
}

@Override
public Persona saveOne(Persona persona) {
String generatedIdentifier = (String) sessionFactory.getCurrentSession().save(persona);
logger.info("generatedIdentifier: {}", generatedIdentifier);
return persona;
}

...

每个 crud 方法都有 sessionFactory.getCurrentSession()

Mockito下面的句子:

when(sessionFactory.getCurrentSession().save(persona)).thenReturn(persona.getId());

总是抛出 java.lang.NullPointerException。我已经确认 sessionFactory.getCurrentSession() 是问题所在。

我已经阅读了以下内容:

因此 java.lang.NullPointerException 被移除。

但我现在总是得到:

org.mockito.exceptions.verification.TooManyActualInvocations: 
sessionFactory.getCurrentSession();
Wanted 1 time:
-> at com.manuel.jordan.repository.hibernate.PersonaHibernateRepositoryTest_.saveOneTest(PersonaHibernateRepositoryTest_.java:76)
But was 2 times. Undesired invocation:
-> at com.manuel.jordan.repository.hibernate.PersonaHibernateRepository.saveOne(PersonaHibernateRepository.java:43)

这两次发生是由于模拟调用和目标调用。

目前我的配置是:

private PersonaHibernateRepository personaHibernateRepository;
private SessionFactory sessionFactory;
private Session session;

...

@Before
public void setup(){
sessionFactory = mock(SessionFactory.class);
session = mock(Session.class);
personaHibernateRepository = new PersonaHibernateRepository(sessionFactory);
//Removes NullPointerException - 'A'
when(sessionFactory.getCurrentSession()).thenReturn(session);
}

@Test
public void saveOneTest(){

//java.lang.NullPointerException removed thanks to 'A'
when(sessionFactory.getCurrentSession().save(persona)).thenReturn(persona.getId());

Persona persona_ = personaHibernateRepository.saveOne(persona);
assertThat(persona_, is(persona));

//B
verify(sessionFactory).getCurrentSession().save(persona);
}

只是玩玩,如果我变了:

来自:verify(sessionFactory).getCurrentSession().save(persona);

到:verify(sessionFactory, times(2)).getCurrentSession().save(persona);(观察times(2))

verify(sessionFactory, times(2)).getCurrentSession().save(persona); 现在 再次出现 java.lang.NullPointerException 抛出; (B)

如果在 @Beforewhen(sessionFactory.getCurrentSession()).thenReturn(session) 更改为 doReturn(session).when( sessionFactory).getCurrentSession()

什么是正确的配置?

最佳答案

setup() 方法中,您正确地告诉 Mockito 在调用 sessionFactory.getCurrentSession() 时返回模拟的 session 实例,因此从他们在你的断言上应该关注 session 实例而不是 sessionFactory。例如:

@Test
public void saveOneTest(){
// you have already told Mockito to return this session instance when sessionFactory.getCurrentSession() is
// invoked so now your when (and optionally verify) should focus on session rather than on sessionFactory
when(session.save(persona)).thenReturn(persona.getId());

Persona persona_ = personaHibernateRepository.saveOne(persona);
assertThat(persona_, is(persona));

verify(session).save(persona);
}

关于java - 正确配置以模拟 Hibernate 的 sessionFactory.getCurrentSession(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45704150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com