Created
April 26, 2020 10:07
-
-
Save yannbriancon/379994b163825ec3abec8a654c3dab11 to your computer and use it in GitHub Desktop.
Integration test asserting the number of queries generated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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