Spring Boot JPA 测试 bean 不在上下文中
SpringBoot JPA test beans not in context
我想不通...出于某种原因 Spring Junit 没有将我的 bean 添加到上下文中。请拜托!!
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的 'com.api.demo.store.FileStorage' 类型的符合条件的 bean:预计至少有 1 个符合自动装配候选条件的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=com.api.demo.store.FileStorageMemDB)}
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 还有 28 个
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileStorageMemDBTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private FileStorage fileStorage;
@Autowired private ApplicationContext ctx;
@Test
public void testRetrieveAll() {
FileInfo fileInfo = new FileInfo("fileOne", "GoodFile.txt", "test");
entityManager.persist(fileInfo);
fileStorage.retrieveAllFiles();
}
}
@Service
public interface FileStorage {
/**
* Stores file in an underlying file store
*
* @param File to store
* @param FileInfo file submission metadata
*/
public void store(MultipartFile mFile, FileInfo info) throws IOException;
/**
* Retrieves a list of all file
*
* @return array containing file metadata
*/
public List<FileInfo> retrieveAllFiles();
/**
* Retrieve file metadata by id
*
* @return file metadata if found
* @Param id of a file to retrieve
*/
public FileInfo retrieveFileMetadata(Long id);
}
/**
* Database implementation of FileStorage interface
*
*/
@Transactional
@Service
public class FileStorageMemDB implements FileStorage {
@PersistenceContext
private EntityManager entityManager;
@Override
public void store(MultipartFile incomingFile, FileInfo info) throws IOException {
......
entityManager.persist(info);
}
@Override
public List<FileInfo> retrieveAllFiles() {
return entityManager.createNamedQuery("query_find_all_records", FileInfo.class).getResultList();
}
@Override
public FileInfo retrieveFileMetadata(Long id) {
return entityManager.find(FileInfo.class, id);
}
}
谢谢瓦伦,
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class FileStorageMemDBTest
{...}
做到了。 !!
我想不通...出于某种原因 Spring Junit 没有将我的 bean 添加到上下文中。请拜托!!
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的 'com.api.demo.store.FileStorage' 类型的符合条件的 bean:预计至少有 1 个符合自动装配候选条件的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=com.api.demo.store.FileStorageMemDB)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ... 还有 28 个
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileStorageMemDBTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private FileStorage fileStorage;
@Autowired private ApplicationContext ctx;
@Test
public void testRetrieveAll() {
FileInfo fileInfo = new FileInfo("fileOne", "GoodFile.txt", "test");
entityManager.persist(fileInfo);
fileStorage.retrieveAllFiles();
}
}
@Service
public interface FileStorage {
/**
* Stores file in an underlying file store
*
* @param File to store
* @param FileInfo file submission metadata
*/
public void store(MultipartFile mFile, FileInfo info) throws IOException;
/**
* Retrieves a list of all file
*
* @return array containing file metadata
*/
public List<FileInfo> retrieveAllFiles();
/**
* Retrieve file metadata by id
*
* @return file metadata if found
* @Param id of a file to retrieve
*/
public FileInfo retrieveFileMetadata(Long id);
}
/**
* Database implementation of FileStorage interface
*
*/
@Transactional
@Service
public class FileStorageMemDB implements FileStorage {
@PersistenceContext
private EntityManager entityManager;
@Override
public void store(MultipartFile incomingFile, FileInfo info) throws IOException {
......
entityManager.persist(info);
}
@Override
public List<FileInfo> retrieveAllFiles() {
return entityManager.createNamedQuery("query_find_all_records", FileInfo.class).getResultList();
}
@Override
public FileInfo retrieveFileMetadata(Long id) {
return entityManager.find(FileInfo.class, id);
}
}
谢谢瓦伦,
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class FileStorageMemDBTest
{...}
做到了。 !!