Spring:@TestConfiguration: Unterschied zwischen den Versionen
Aus Alexander's Wiki
Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<syntaxhighlight lang="java" line="1"> @TestConfiguration static class EmployeeServiceImplTestContextConfiguration { @Bean public Comm…“) |
Admin (Diskussion | Beiträge) |
||
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
<syntaxhighlight lang="java" line="1"> | == Abweichende Properties-Datei == | ||
Annotation in der Testklasse um die ''application-text.yaml'' Datei zu verwenden:<syntaxhighlight lang="java" line="1"> | |||
@ActiveProfiles(profiles = "test") | |||
</syntaxhighlight>Überprüfung während des Starts der AprinApplication:<syntaxhighlight lang="java" line="1"> | |||
INFO [main] o.s.b.SpringApplication: The following profiles are active: test | |||
</syntaxhighlight> | |||
== Spezieller ApplicationContext == | |||
Zu überprüfen:<syntaxhighlight lang="java" line="1"> | |||
@Import(CommentServiceTest.EmployeeServiceImplTestContextConfiguration.class) | |||
</syntaxhighlight>Via Annotation in Spring Boot:<syntaxhighlight lang="java"> | |||
@SpringBootTest(classes = { BaseMapperImpl.class, PropositionMapperImpl.class, DocumentService.class}) | |||
</syntaxhighlight>Ganz explizit:<syntaxhighlight lang="java" line="1"> | |||
@TestConfiguration | @TestConfiguration | ||
static class EmployeeServiceImplTestContextConfiguration { | static class EmployeeServiceImplTestContextConfiguration { | ||
@Bean | @Bean | ||
public | public DocumentService documentService() | ||
return new | { | ||
return new DocumentService(); | |||
} | |||
@Bean("baseMapperBean") | |||
public BaseMapper baseMapper() | |||
{ | |||
return Mappers.getMapper(BaseMapper.class); | |||
} | } | ||
@Bean("propositionMapperBean") | |||
@DependsOn({"baseMapperBean"}) | |||
public PropositionMapper propositionMapper() | |||
{ | |||
return Mappers.getMapper(PropositionMapper.class); | |||
} | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Aktuelle Version vom 13. Dezember 2021, 13:15 Uhr
Abweichende Properties-Datei
Annotation in der Testklasse um die application-text.yaml Datei zu verwenden:
@ActiveProfiles(profiles = "test")
Überprüfung während des Starts der AprinApplication:
INFO [main] o.s.b.SpringApplication: The following profiles are active: test
Spezieller ApplicationContext
Zu überprüfen:
@Import(CommentServiceTest.EmployeeServiceImplTestContextConfiguration.class)
Via Annotation in Spring Boot:
@SpringBootTest(classes = { BaseMapperImpl.class, PropositionMapperImpl.class, DocumentService.class})
Ganz explizit:
@TestConfiguration
static class EmployeeServiceImplTestContextConfiguration {
@Bean
public DocumentService documentService()
{
return new DocumentService();
}
@Bean("baseMapperBean")
public BaseMapper baseMapper()
{
return Mappers.getMapper(BaseMapper.class);
}
@Bean("propositionMapperBean")
@DependsOn({"baseMapperBean"})
public PropositionMapper propositionMapper()
{
return Mappers.getMapper(PropositionMapper.class);
}
}