Spring:@TestConfiguration: Unterschied zwischen den Versionen

Aus Alexander's Wiki
 
Zeile 7: Zeile 7:


== Spezieller ApplicationContext ==
== Spezieller ApplicationContext ==
Annotation der Test-Klasse<syntaxhighlight lang="java" line="1">
Zu überprüfen:<syntaxhighlight lang="java" line="1">
@Import(CommentServiceTest.EmployeeServiceImplTestContextConfiguration.class)
@Import(CommentServiceTest.EmployeeServiceImplTestContextConfiguration.class)
</syntaxhighlight><syntaxhighlight lang="java" line="1">
</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 CommentMapper employeeService() {
         public DocumentService documentService()
             return new CommentMapper() {
        {
                // implement methods
             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);
        }

    }