Skip to content

Instantly share code, notes, and snippets.

@yannbriancon
Last active May 8, 2020 14:44
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/7cf45cab19c050d7688171f72e329dfc to your computer and use it in GitHub Desktop.
Save yannbriancon/7cf45cab19c050d7688171f72e329dfc to your computer and use it in GitHub Desktop.
Test triggering N+1 query
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@Transactional
class NPlusOneQueriesLoggingTest {
@Autowired
private MessageRepository messageRepository;
@Test
void nPlusOneQueriesDetection_isLoggingWhenDetectingNPlusOneQueries() {
// Fetch the messages without the authors
List<Message> messages = messageRepository.findAll();
// Trigger N+1 queries
List<String> names = messages.stream()
.map(message -> message.getAuthor().getName())
.collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment