如何在没有@datajpatest的春季框架中测试Crudrepository
我想在Spring Framework应用程序中为以下UserRepository编写单元测试:
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
User findByEmail(String email);
}
但是CanFinduserByeMail()测试失败是因为
@ExtendWith(MockitoExtension.class)
public class UserRepositoryTest {
@Autowired
private UserRepository underTest;
@Test
public void canFindUserByEmail() {
// given
User expectedUser = new User();
expectedUser.setEmail("[email protected]");
underTest.save(expectedUser);
// when
final User actualUser = underTest.findByEmail(expectedUser.getEmail());
// then
assertThat(actualUser).isEqualTo(expectedUser);
}
}
当我添加下一个注释时承诺是无效的,但是它们是否打算用于集成测试,而不是用于单元测试?
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringConfig.class})
@WebAppConfiguration()
谢谢!
I wanted to write unit test for the following UserRepository in Spring Framework application:
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
User findByEmail(String email);
}
But canFindUserByEmail() test fails because underTest is null
@ExtendWith(MockitoExtension.class)
public class UserRepositoryTest {
@Autowired
private UserRepository underTest;
@Test
public void canFindUserByEmail() {
// given
User expectedUser = new User();
expectedUser.setEmail("[email protected]");
underTest.save(expectedUser);
// when
final User actualUser = underTest.findByEmail(expectedUser.getEmail());
// then
assertThat(actualUser).isEqualTo(expectedUser);
}
}
When I add the next annotations it works, but are they intended for integration testing, not for unit testing?
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringConfig.class})
@WebAppConfiguration()
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论