Skip to content

Commit

Permalink
improve create junit integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 6, 2024
1 parent c8661f9 commit d4fd63a
Showing 1 changed file with 41 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ _%>

@Test<%= transactionalAnnotation %>
void create<%= entityClass %>() throws Exception {
int databaseSizeBeforeCreate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();
long databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand All @@ -668,31 +668,46 @@ _%>
<%= dtoClass %> <%= dtoInstance %> = <%= entityInstance %>Mapper.toDto(<%= persistInstance %>);
<%_ } _%>
<%_ if (reactive) { _%>
webTestClient.post().uri(ENTITY_API_URL)
var returned<%- restClass %> = webTestClient.post().uri(ENTITY_API_URL)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(om.writeValueAsBytes(<%= restInstance %>))
.exchange()
.expectStatus().isCreated();
.expectStatus()
.isCreated()
.expectBody(<%- persistClass %>.class)
.returnResult()
.getResponseBody();
<%_ } else { _%>
rest<%= entityClass %>MockMvc.perform(post(ENTITY_API_URL)<% if (authenticationUsesCsrf) { %>.with(csrf())<% }%>
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(<%= restInstance %>)))
.andExpect(status().isCreated());
var returned<%- restClass %> = om.readValue(
rest<%= entityClass %>MockMvc
.perform(
post(ENTITY_API_URL)
<%_ if (authenticationUsesCsrf) { _%>
.with(csrf())
<%_ } _%>
.contentType(MediaType.APPLICATION_JSON)
.content(om.writeValueAsBytes(<%= restInstance %>))
)
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getContentAsString(),
<%- restClass %>.class
);
<%_ } _%>

// Validate the <%= entityClass %> in the database
<%_ if (databaseTypeCouchbase) { _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeCreate + 1);
assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
<%_ if (searchEngineElasticsearch) { _%>
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
int searchDatabaseSizeAfter = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
assertThat(searchDatabaseSizeAfter).isEqualTo(searchDatabaseSizeBefore + 1);
});
<%_ } _%>
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>Repository.findById(returned<%- restClass %>.get<%- primaryKey.nameCapitalized %>())<%= reactive ? callBlock : '.orElseThrow()' %>;
<%_ for (const field of fieldsToTest) {
if (field.fieldTypeZonedDateTime) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= 'DEFAULT_' + field.fieldNameUnderscored.toUpperCase() %>);
Expand Down Expand Up @@ -724,7 +739,7 @@ _%>
<%= dtoClass %> <%= dtoInstance %> = <%= entityInstance %>Mapper.toDto(<%= persistInstance %>);
<%_ } _%>

int databaseSizeBeforeCreate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();
long databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand All @@ -748,7 +763,7 @@ _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeCreate);
assertSameRepositoryCount(databaseSizeBeforeCreate);
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeAfter = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
assertThat(searchDatabaseSizeAfter).isEqualTo(searchDatabaseSizeBefore);
Expand All @@ -761,7 +776,7 @@ _%>
// Initialize the database
<%= entityInstance %>Repository.<%= saveMethod %>(<%= persistInstance %>)<%= callBlock %>;
<%_ const alreadyGeneratedEntities = []; _%>
int databaseSizeBeforeCreate = <%= entityInstance %>Repository.findAll()<%= callListBlock %>.size();
long databaseSizeBeforeCreate = getRepositoryCount();
<%_ if (searchEngineElasticsearch) { _%>
int searchDatabaseSizeBefore = IterableUtil.sizeOf(<%= entityInstance %>SearchRepository.findAll()<%= callListBlock %>);
<%_ } _%>
Expand Down Expand Up @@ -816,7 +831,7 @@ _%>
SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
List<<%= persistClass %>> <%= entityInstance %>List = <%= entityInstance %>Repository.findAll()<%= callListBlock %>;
assertThat(<%= entityInstance %>List).hasSize(databaseSizeBeforeCreate);
assertSameRepositoryCount(databaseSizeBeforeCreate);
<%= persistClass %> test<%= entityClass %> = <%= entityInstance %>List.get(<%= entityInstance %>List.size() - 1);

// Validate the id for MapsId, the ids must be same
Expand Down Expand Up @@ -1865,4 +1880,16 @@ _%>
if (!field.fieldTypeString) { %>.toString()<% } %>))<%= !reactive ? ')' : '' %><%_ } _%>;
}
<%_ } _%>

protected long getRepositoryCount() {
return <%= entityInstance %>Repository.count()<%= callBlock %>;
}

protected void assertIncrementedRepositoryCount(long countBefore) {
assertThat(countBefore + 1).isEqualTo(getRepositoryCount());
}

protected void assertSameRepositoryCount(long countBefore) {
assertThat(countBefore).isEqualTo(getRepositoryCount());
}
}

0 comments on commit d4fd63a

Please sign in to comment.