我有一个Spring Boot应用程序(1.5.10.RELEASE),其中包含一个main(SpringBootApplication),如下所示:
@SpringBootApplication
@Configuration
@EntityScan(basePackages = { "db.modell", "db.modell.base" })
@ComponentScan(basePackages = { "de.gui.test" })
public class SpringBootConsoleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
}
和两个REST控制器如下:
@RestController
@RequestMapping("/as")
public class AController {
@Autowired
private ARepository aRepository;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Collection<A>> getAs() {
return new ResponseEntity<>(orgtFarbeRepository.findAll(), HttpStatus.OK);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<A> getA(@PathVariable long id) {
A a = ARepository.findOne(id);
if (party != null) {
return new ResponseEntity<>(ARepository.findOne(id), HttpStatus.OK);
} else {
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
}
}
此外,我有一个这样的测试:
@RunWith(SpringRunner.class)
@WebMvcTest(AController.class)
public class AControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private ARepository ARepository;
@Test
public void firstTest() throws Exception {
A a = new aFarbe();
a.set....
when(ARepository.findAll()).thenReturn(Collections.singleton(a));
mvc.perform(
get("/as")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
)
.andExpect(status().isOk());
}
}
存储库看起来像这样:
public interface ARepository extends CrudRepository<A, Long>
{
Collection<A> findAll();
}
public interface BRepository extends CrudRepository<B, Long>
{
Collection<B> findAll();
}
A和B他们自己是JPA注释类。整个应用程序包含对数据库的访问..
此外,我有这样的服务:
@Service
public class XService {
private static final Logger LOGGER = LoggerFactory.getLogger(XService.class);
@Autowired
private ARepository aRepository;
@Autowired
private BRepository bRepository;
...
}
XService不是通过@Autowire左右使用的(只需删除它):
所以我尝试运行AControllerTest,我收到以下错误:
java.lang.IllegalStateException:无法加载ApplicationContext org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) .. .. 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 引起: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“XService”的bean时出错:不满意的依赖项 通过字段“BRepository”表达;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 可以使用'BRepository'类型的限定bean:预计至少为1 豆有资格作为autowire候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} .. .. 在 org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 26更多引起: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 可以使用'BRepository'类型的限定bean:预计至少为1 豆有资格作为autowire候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)} 在 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) ... 44更多
我的假设是,在测试期间,更多的上下文开始了。问题是如何防止这种情况?这意味着只启动AControler的上下文而已?我认为基于@WebMvcTest(AController.class)
它应该是有限的,看起来不是这样......
答案 0 :(得分:2)
引用的答案并没有真正回答我的问题,但在上下文中hint给了我解决方案。这意味着在我的测试中添加以下内容:
所以我必须添加@OverrideAutoConfiguration(enabled=true)
:
@RunWith(SpringRunner.class)
@WebMvcTest(OrgtFarbenController.class)
@OverrideAutoConfiguration(enabled=true)
public class AControllerTest {
...
}