mardi 22 août 2017

Repositories.getRepositoryFor() returns null in SpringBootTest

My goal is to test annotation processor. To do this I decided to create some test data with annotations in test package. My annotation processor uses Spring Repositories, but they return null every time, even if the repository exists. In debug I noticed that my repositories do not exist in applicationContext. How can I force Spring to see my repositories?

My Test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AnnotationConfigWebApplicationContext.class, 
RepoConfig.class, AnnotationProcessor.class}, loader = AnnotationConfigContextLoader.class)
public class AnnotationProcessorTest{

@Autowired
private AnnotationProcessor processor;

@Test
public void test(){
    assertNotNull(processor.getRepositories()); //Repositories is empty
}
}

Repository:

@Repository
public interface MyRepository extends PagingAndSortingRepository<MyClass, 
Serializable> {
}

MyClass:

@Entity
public class MyClass implements Serializable{

    @Id
    private String ref;

    private String field;
}

RepoConfig:

@Configuration
@EnableJpaRepositories(basePackageClasses = {MyRepository.class})
public class RepoConfig{
}

Aucun commentaire:

Enregistrer un commentaire