Skip to content

Instantly share code, notes, and snippets.

@yannbriancon
Created April 26, 2020 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yannbriancon/379994b163825ec3abec8a654c3dab11 to your computer and use it in GitHub Desktop.
Save yannbriancon/379994b163825ec3abec8a654c3dab11 to your computer and use it in GitHub Desktop.
Integration test asserting the number of queries generated
...
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class NotificationResourceIntTest {
@Autowired
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor;
@Test
public void saveFile_isOk() throws Exception {
// Initialize the query to 0 and allow the counting
hibernateQueryCountInterceptor.startCounter();
// Call the resource that we want to test
MvcResult result = mvc.perform(get("/rest/notifications"))
.andExpect(status().isOk())
.andReturn();
// Get the query count for this thread and check that it is equal to the number of query you expect, let's say 4.
// The count is checked and we detect potential n+1 queries.
Assertions.assertThat(hibernateQueryCountInterceptor.getQueryCount()).isEqualTo(4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment