Skip to content

Instantly share code, notes, and snippets.

@bmchild
Created March 1, 2012 23:27
Show Gist options
  • Save bmchild/1953988 to your computer and use it in GitHub Desktop.
Save bmchild/1953988 to your computer and use it in GitHub Desktop.
An example of using @configuration for testing spring repositories
package com.bmchild.repository;
/*imports
*.
*.
*.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class MyRepositoryIntegrationTest {
@Configuration
@ImportResource("classpath:/test-context.xml")
static class ContextConfiguration {
/*
* MyOtherRepository has a custom impl
*/
@Bean
public MyOtherCustomRepository myOtherCustomRepository() {
MyOtherCustomRepository repo = new MyOtherRepositoryImpl();
return repo;
}
@Bean
public JpaRepositoryFactoryBean<MyRepository,MyEntity,Long> getMyRepositoryBean() {
JpaRepositoryFactoryBean<MyRepository,MyEntity,Long> rfb = new JpaRepositoryFactoryBean<MyRepository,MyEntity,Long>();
rfb.setRepositoryInterface(MyRepository.class);
rfb.setTransactionManager("transactionManagerBeanName");
return rfb;
}
@Bean
public JpaRepositoryFactoryBean<MyOtherRepository,MyOtherEntity,Long> getMyOtherRepositoryBean() {
JpaRepositoryFactoryBean<MyOtherRepository,MyOtherEntity,Long> rfb =
new JpaRepositoryFactoryBean<MyOtherRepository,MyOtherEntity,Long>();
rfb.setRepositoryInterface(ManPowerProfileRepository.class);
rfb.setTransactionManager("transactionManagerBeanName");
rfb.setCustomImplementation(myOtherCustomRepository());
return rfb;
}
}
@Autowired
private MyRepository myRepository;
@Autowired
private MyOtherRepository myOtherRepository;
/*tests
*.
*.
*.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment