2015-04-15 6 views
7

Najważniejsze jest to, że struktura testowa Spring Batch (v2) ma JobLauncherTestUtils.setJob z adnotacją @Autowired. Nasz zestaw testów ma wielu dostawców klasy Job. Ponieważ ta klasa nie jest czymś, co mogę zmodyfikować, nie jestem pewna, w jaki sposób mogę zakwalifikować się do tego, z którą robotą zostanie wykonana autopsja, która może być inna w każdym teście.W jaki sposób mogę zakwalifikować program ustawiający, który nie jest "właścicielem"?

STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherTestUtilsForSnapshot': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: coverageRuleBatch,generateMetricsSnapshotJob 

Próbowałem dodanie tego JavaConfig który jest rozpoznawany, ale błąd mówi, że nadal autocalling setJob

@Configuration 
public class SpringTestConfiguration 
{ 
@Bean 
public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot(final Job generateMetricsSnapshotJob) 
{ 
    JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils(); 
    jobLauncherTestUtils.setJob(generateMetricsSnapshotJob); 
    return jobLauncherTestUtils; 
} 
} 

notatka: Nie wymaga rozwiązania JavaConfig, ale byłoby miło . Chciałbym też, jeśli to możliwe, nadal Autowire pól takich jak JobRepository, ponieważ jest tylko jeden.

+0

http://stackoverflow.com/q/22416140/206466 powiązane, może duplikować – xenoterracide

Odpowiedz

2

roztworze wymyśliłem

@Configuration 
public class SpringBatchTestConfiguration 
{ 
@Bean 
public static JobLauncherTestUtils jobLauncherTestUtilsForSnapshot() 
{ 
    return new SnapshotJobLauncherTestUtils(); 
} 

public static class SnapshotJobLauncherTestUtils extends JobLauncherTestUtils 
{ 
    @Override 
    @Qualifier("generateMetricsSnapshotJob") 
    public void setJob(final Job job) 
    { 
     super.setJob(job); 
    } 
} 
} 

i testu końcowego

@Autowired 
@Qualifier("jobLauncherTestUtilsForSnapshot") 
protected JobLauncherTestUtils jobLauncherTestUtils; 

Jestem dość pewny, że mogę tylko opisać moje TestUtils za pomocą @Component i nazwać go poprawnie i zrobić to samo.

0

Może mógłbyś użyć do tego profili wiosennych. Przypisać inny profil do każdego Job dostawcy definicji fasoli (z dopiskiem @Profile("profileName"), a następnie aktywować profil dla właściwego operatora na określonej klasy testowej z dopiskiem @ActiveProfiles("profileName").

2

moje rozwiązanie, kiedy wpadłem na ten sam problem, było ograniczenie komponentu-skan tak, że tylko jedna fasola Praca jest tworzony w ramach testu.

@Configuration 
@ComponentScan(basePackages={ 
    "com.example.batch.jobs.metrics", //package where generateMetricsSnapshotJob is the only job 
    "com.example.batch.common", 
    "..." 
}) 
public class SpringTestConfiguration 
{ 
    @Bean 
    public JobLauncherTestUtils jobLauncherTestUtils() 
    { 
     //generateMetricsSnapshotJob and other requirements will be autowired 
     return new JobLauncherTestUtils(); 
    } 
} 

Być może trzeba dostosować swoją strukturę pakietów dla tej pracy.

0
  1. Można rozszerzyć metodę automatycznego wstrzykiwania AutowiredAnnotationBeanPostProcessor i zastąpić metodę inject.

  2. Usuń <context:scan .. /> wpisy

  3. Zarejestruj swoją fasola <bean class ="a.b.CustomAutowiredAnnotationBeanPostProcessor" />

0

Innym rodzajem rozwiązania jest wstrzykiwanie go za pomocą settera. Preferuję to rozwiązanie, ponieważ jest bardziej przejrzyste i łatwiejsze.

@Configuration 
public class SpringTestConfiguration 
{ 
    @Bean 
    public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot() 
    { 
     return new JobLauncherTestUtils() { 
      @Override 
      @Autowired 
      public void setJob(@Qualifier("generateMetricsSnapshotJob") Job job) { 
       super.setJob(job); 
      } 
     }; 
    } 
} 
Powiązane problemy