Chr*_*der 5 java sql jpa eclipselink sniffy
我正在使用JPA,通过EclipseLink.在我的单元测试中,我想测试在操作期间执行了多少SQL查询.这样,如果稍后的修改导致查询计数爆炸(例如,如果触发延迟加载),则单元测试将其标记为可能需要优化.
我正在寻找正确的API来做到这一点.纯JPA解决方案是理想的,但我可以在单元测试中使用特定于EclipseLink的API.我查看了EclipseLink分析器,但它似乎没有给我一种方法来计算SQL查询的数量.
在此先感谢您的帮助!
我没有找到合适的工具来进行此类验证,因此创建了自己的工具。它被称为sniffy,可在 MIT 许可下使用。
您可以断言生成的查询的数量,如下所示:
// Integrate Sniffy to your test using @Rule annotation and a QueryCounter field
@Rule
public final QueryCounter queryCounter = new QueryCounter();
// Now just add @Expectation or @Expectations annotations to define number of queries allowed for given method
@Test
@Expectation(1)
public void testJUnitIntegration() throws SQLException {
// Just add sniffer: in front of your JDBC connection URL in order to enable sniffer
final Connection connection = DriverManager.getConnection("sniffer:jdbc:h2:mem:", "sa", "sa");
// Do not make any changes in your code - just add the @Rule QueryCounter and put annotations on your test method
connection.createStatement().execute("SELECT 1 FROM DUAL");
}
Run Code Online (Sandbox Code Playgroud)
有关与 JUnit 集成的更多信息,请参阅项目 wiki
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |