Spring:Mocking: Unterschied zwischen den Versionen
Aus Alexander's Wiki
Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<syntaxhighlight lang="java"> @Spy private ItemMapper itemMapper = Mappers.getMapper(ItemMapper.class); </syntaxhighlight>Or alternativly to enable spring de…“) |
Admin (Diskussion | Beiträge) |
||
Zeile 4: | Zeile 4: | ||
</syntaxhighlight>Or alternativly to enable spring dependency injection via<syntaxhighlight lang="java"> | </syntaxhighlight>Or alternativly to enable spring dependency injection via<syntaxhighlight lang="java"> | ||
@SpringBootTest(classes = {ItemMapperImpl.class}). | @SpringBootTest(classes = {ItemMapperImpl.class}). | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Beispiele === | |||
<syntaxhighlight lang="java" line="1"> | |||
Mockito.when(compoundDocumentRepository.save(Mockito.any(CompoundDocumentEntity.class))) | |||
.thenAnswer(invocation -> | |||
{ | |||
CompoundDocumentEntity compoundDocumentEntity = invocation.getArgument(0); | |||
compoundDocumentEntity.setCreatedAt(Instant.now()); | |||
compoundDocumentEntity.setUpdatedAt(Instant.now()); | |||
return compoundDocumentEntity; | |||
} | |||
); | |||
</syntaxhighlight><syntaxhighlight lang="java"> | |||
Mockito.when(session.getUser()).thenReturn(user); | |||
</syntaxhighlight><syntaxhighlight lang="java"> | |||
ReflectionTestUtils.setField(compoundDocument, "dbId", 5L); | |||
</syntaxhighlight> |
Version vom 21. Februar 2022, 13:05 Uhr
@Spy
private ItemMapper itemMapper = Mappers.getMapper(ItemMapper.class);
Or alternativly to enable spring dependency injection via
@SpringBootTest(classes = {ItemMapperImpl.class}).
Beispiele
Mockito.when(compoundDocumentRepository.save(Mockito.any(CompoundDocumentEntity.class)))
.thenAnswer(invocation ->
{
CompoundDocumentEntity compoundDocumentEntity = invocation.getArgument(0);
compoundDocumentEntity.setCreatedAt(Instant.now());
compoundDocumentEntity.setUpdatedAt(Instant.now());
return compoundDocumentEntity;
}
);
Mockito.when(session.getUser()).thenReturn(user);
ReflectionTestUtils.setField(compoundDocument, "dbId", 5L);