Sprint Boot JPA 实体单元测试
假设我有一个带有 spring-boot-starter-data-jpa
的 Spring Boot 应用程序,
我有一个实体
Entity
@Table(name = "STUDENT")
@Getter
public class Student {
@Id
@Column(name="ID") private int id;
@Column(name="NAME") private String name;
@Column(name="DESCRIPTION") private String description;
}
和一个存储库
public interface StudentRepository extends JpaRepository<Student, Integer> {
Optional<Student> findByName(String name);
}
,我知道我不必对存储库进行单元测试,因为没有代码或自定义查询并且 jpa 应该可以工作。
如何测试表和列映射是否正确?
测试这一点有用吗?想象一下,在 dev 中它工作正常,但在 acc 中,dba 在 NAME
列中出现了拼写错误。如果我不测试任何内容,应用程序将启动,但在调用 findByName
方法时会抛出异常。
我尝试过类似的东西
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class DocTypeRepositoryTest {
@Autowired
private StudentRepository studentRepository;
@Test
public void findByName() {
String name = "John";
docTypeRepository.findByName(name);
}
}
,但这是一个好的做法吗?
另一个小问题:@DataJpaTest 仍然是单元测试还是集成测试?
Let say I have a Spring Boot app with spring-boot-starter-data-jpa
I have an entity
Entity
@Table(name = "STUDENT")
@Getter
public class Student {
@Id
@Column(name="ID") private int id;
@Column(name="NAME") private String name;
@Column(name="DESCRIPTION") private String description;
}
and a repository
public interface StudentRepository extends JpaRepository<Student, Integer> {
Optional<Student> findByName(String name);
}
I understand that I do not have to unit test the repository as there is no code or custom query and that jpa is supposed to work.
How can I test that the table and column mapping is correct ?
Is it usefulness to test that ? Imagine in dev it's working fine but in acc the dba made a typo with the NAME
column. If I do not test anything the application will start but will throw an exception when calling the findByName
method.
I tried with something like
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class DocTypeRepositoryTest {
@Autowired
private StudentRepository studentRepository;
@Test
public void findByName() {
String name = "John";
docTypeRepository.findByName(name);
}
}
But is it a good practice ?
Another little question : with @DataJpaTest
is it still a unit test or is it an integration test ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应用程序之外的每个外部组件都可以这样进行测试,但这是单元测试吗?当然不再是了。这是一个集成测试。它验证您是否能够使用集成系统(数据库、文件、ESB 等)进行一些操作,并且它们工作正常。
因此,如果您犯了一些拼写错误,这是您的责任,而不是您正在使用的集成组件/系统的责任。
如果集成测试有用,那么当您的集成组件/系统不断发展并且您的应用程序无法再执行某些任务时,就会感到羞耻。如果您没有及时注意到这个错误,我敢打赌您的客户会非常生气。
Every external component outside of your application can be tested like that, but is this a unit test ? Certainly not anymore. It is an integration test. It verifies you are able to make some operations with the integrated system (databases, files, ESB, etc.) and they are working fine.
So if you make some typos errors, it is your responsability, not the responsability of the integrated components/systems you are using.
If integrated tests are usefullness, shame is in the air when your integrated component/system is evolving and your application is not able to do some tasks anymore. If you did not notice this bug in time, I bet your clients will be angry a lot.