Spring data jpa 如何测试jpa只读查询?

Spring data jpa 如何测试jpa只读查询?,spring-data-jpa,spring-data,Spring Data Jpa,Spring Data,针对该问题对模型进行了简化 我有这个实体: @Entity public class Formation { @Id Long id; String login; String code; String level; // geters and Setters 使用此存储库: public interface FormationRepository extends JpaRepository<Formation, Long> { @

针对该问题对模型进行了简化

我有这个实体:

@Entity
public class Formation {
    @Id
    Long id;
    String login;
    String code;
    String level;
    // geters and Setters
使用此存储库:

public interface FormationRepository extends JpaRepository<Formation, Long> {

@Query(value = "SELECT UNIQUE l.id, l.login, d.code,d.level\n" 
        "   FROM          table_login l,\n" +
        "                 table_diploma d,\n" +
        "   WHERE         
        "                 l.fhab_key = d.fhab_key\n" +
        "   AND           l.login= :login", nativeQuery = true)
List<Formation> findAllByLogin(@Param("login")String login);
这里我需要补充一点,我不想在我的应用程序中使用sql文件

那么这里的解决方案是什么呢

是否更改我的模型以按表创建实体?(在现实中,我的请求使用了8个内部连接,因此编写所有这些连接都需要相当长的时间…)


另一种解决方案?

如果不想为所有表创建实体,可以通过在类路径上定义
schema.sql
data.sql
预先填充测试H2数据库

  • schema.sql将包含DDL语句来创建所有涉及的表(您提到的8个表)
  • data.sql将包含使自定义sql返回login=123所需的insert语句
你可以找到一个例子

那么您的测试代码如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class FormationRepositoryTest {
    @Autowired
    FormationRepository formationRepository;

    @Test
    public void communeRepositoryTest() {
        List<Formation> formations =  formationRepository.findAllByLogin("123"); 
        Assert.assertEquals(formations.size(), 1); 
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest(类=Application.class)
@DirtiesContext(classMode=DirtiesContext.classMode.AFTER\u每个\u测试\u方法)
公共类FormationRepositoryTest{
@自动连线
格式化存储库格式化存储库;
@试验
公共无效公共存储测试(){
List formations=formationRepository.findAllByLogin(“123”);
Assert.assertEquals(formations.size(),1);
}
}

如果不想为所有表创建实体,可以通过在类路径上定义
schema.sql
data.sql
预先填充测试H2数据库

  • schema.sql将包含DDL语句来创建所有涉及的表(您提到的8个表)
  • data.sql将包含使自定义sql返回login=123所需的insert语句
你可以找到一个例子

那么您的测试代码如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class FormationRepositoryTest {
    @Autowired
    FormationRepository formationRepository;

    @Test
    public void communeRepositoryTest() {
        List<Formation> formations =  formationRepository.findAllByLogin("123"); 
        Assert.assertEquals(formations.size(), 1); 
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest(类=Application.class)
@DirtiesContext(classMode=DirtiesContext.classMode.AFTER\u每个\u测试\u方法)
公共类FormationRepositoryTest{
@自动连线
格式化存储库格式化存储库;
@试验
公共无效公共存储测试(){
List formations=formationRepository.findAllByLogin(“123”);
Assert.assertEquals(formations.size(),1);
}
}

我希望避免使用sql文件。主要是因为,当jpa能够处理sql和实体时,维护sql和实体是一件痛苦的事情。但也许这是最“合理”的方法……我希望避免使用sql文件。主要是因为,当jpa能够处理sql和实体时,维护sql和实体是一件痛苦的事情。但也许这是最“合理”的方法。。。