I have a spring-data-jpa
repository called TagRepository
. My spring-boot
version is 2.1.2. I want to write a DataJpaTest
for it. I have written the following code:
@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(classes={TagRepository.class})
public class TagRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private TagRepository tagRepository;
@Test
public void findByTagTest() {
Tag tag = new Tag("java");
testEntityManager.persistAndFlush(tag);
Optional<Tag> optionalTag = tagRepository.findByTag(tag.getTag());
if(!optionalTag.isPresent()) {
fail("Tag not found hence test failed");
}
assertThat(optionalTag.get()).isEqualTo(tag);
}
}
However, when I execute the test it says Application failed to start and I get following error:
The bean 'tagRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
Aucun commentaire:
Enregistrer un commentaire