- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试过 @JdbcTest
关于以下项目结构。
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ ├── Application.java
│ │ ├── repository
│ │ │ └── DemoRepository.java
│ │ └── service
│ │ └── DemoService.java
│ └── resources
│ └── application.properties
└── test
└── java
└── com
└── example
├── ApplicationTests.java
└── repository
└── DemoRepositoryTests.java
package com.example.repository;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
@Repository
public class DemoRepository {
private final JdbcOperations jdbcOperations;
DemoRepository(JdbcOperations jdbcOperations) {
this.jdbcOperations = jdbcOperations;
}
public int findNumber() {
return jdbcOperations.queryForObject("SELECT 1",int.class);
}
}
package com.example.repository;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@JdbcTest
@Import(DemoRepository.class)
public class DemoRepositoryTests {
@Autowired
private DemoRepository repository;
@Test
public void findNumber() {
Assertions.assertThat(repository.findNumber()).isEqualTo(1);
}
}
@SpringBootApplication
)
package com.example;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.example.service.DemoService;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public CommandLineRunner demo(DemoService service) {
return args -> System.out.println("Number:" + service.findNumber());
}
}
DemoRepositoryTests
.然后发生了以下错误。
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-10 23:55:44.092 ERROR 9375 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method demo in com.example.Application required a bean of type 'com.example.service.DemoService' that could not be found.
Action:
Consider defining a bean of type 'com.example.service.DemoService' in your configuration.
2017-03-10 23:55:44.100 ERROR 9375 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1442d7b5] to prepare test instance [com.example.repository.DemoRepositoryTests@3c0a50da]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:47) ~[spring-boot-test-autoconfigure-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in com.example.Application: Unsatisfied dependency expressed through method 'demo' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) ~[spring-boot-test-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 28 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 46 common frames omitted
最佳答案
我在尝试使用 @JdbcTest
为我的存储库设置测试方法时遇到了同样的问题。 .
这是我在查看 Spring Boot 引用文档和 Andy Wilkinson 引用的源代码后的解决方案。
@JdbcTest
搜索用 @SpringBootApplication
注释的主要配置, 和其他 @Configuration
通过声明用 @Configuration
注释的内部类.内部@Configuration
根据以下代码,类将覆盖为当前为空的主要配置。 @RunWith(SpringRunner.class)
@JdbcTest
@Import({TestConfiguration.class, DemoRepository.class})
@AutoConfigureTestDatabase
@TestPropertySource(properties = {"spring.datasource.schema=classpath:dbschema.sql"})
public class DemoRepositoryTest {
//This is the main key to override the search done by @JdbcTest to
//those annotated with @SpringBootApplication, other @Configuration
@Configuration
@EnableAutoConfiguration
static class Config {
}
@Autowired
DemoRepository repository;
@Before
public void setUp() throws Exception {
...
}
@Test
public void findOne() throws Exception {
...
}
}
NamedParameterJdbcDaoSupport
,所以这里是我如何配置 DataSource
@TestConfiguration
public class TestConfiguration {
//SpringBoot Test AutoConfig will inject the memory db here.
@Autowired
DataSource dataSource;
@Bean
public NamedParameterJdbcDaoSupport jdbcDaoSupport() {
NamedParameterJdbcDaoSupport support = new NamedParameterJdbcDaoSupport();
support.setDataSource(dataSource);
return support;
}
}
DemoRepository.java
@Repository
public class DemoRepository {
private final NamedParameterJdbcTemplate jdbcTemplate;
public DemoRepository(NamedParameterJdbcDaoSupport jdbcDaoSupport) {
this.jdbcTemplate = jdbcDaoSupport.getNamedParameterJdbcTemplate();
}
pom.xml
中声明内存 DB 驱动程序, h2 以这里为例:<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
<version>1.4.196</version>
</dependency>
关于spring-boot - @JdbcTest 检测带有@SpringBootApplication 注释的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42722480/
我在阻止 Spring Boot 自动配置某些类(在本例中:SolrAutoConfiguration)时遇到了一些困难。为了说明这一点,我设置了一个简化得多的示例: https://github.c
我在阻止 Spring Boot 自动配置某些类时遇到了一些困难(在本例中:SolrAutoConfiguration)。为了说明,我设置了一个大大简化的示例: https://github.com/
根据我的理解,使用@SpringBootApplication相当于拥有@EnableAutoConfiguration和@ComponentScan。由于这个原因,我不明白为什么Spring找不到我
静态文件位于: src/main/resources/static/ js CSS 图片 等.. 无论我做什么,文件都不会被拾取。我试过 addResourceHandlers 但似乎没有任何效果。
我写了一个基于 Spring Boot 的应用程序,但是当我将所有类(模型、用 @restController 注释的 Controller )放在 SpringBoot 存在的同一个包中时,它就可以
我在 nexus 上发布了一个具有包 ID 的公共(public)库 x.xx.common 包含常见Feign客户端代理接口(interface)子包 使用这个库的项目有包 id。 x.xx.acc
我正在努力在简单的 SpringBootApplication 中加载配置。当我尝试打印配置类中的变量时,它们始终为空。 最初,我在 WatchApplication.java 中有可配置变量。然而,
我的 Spring Boot 配置有问题。 我使用 https://start.spring.io/ 创建了基础 Spring Boot 项目 我有一个问题,配置仅适用于子目录中的类: 我尝试过注释@
使用@WebMvcTest 将通过查找@SpringBootConfiguration 类(例如@SpringBootApplication)自动配置所有web 层bean。 如果配置类在不同的包里,
我有一个带有 Autowiring 功能的简单 Spring 应用程序 AppMain,我不想在其中使用任何显式 XML 配置。 在我的 TaskScheduler 组件中,我想将 ThreadPoo
没有@SpringBootApplication,Spring boot应用程序如何工作? 在我们的项目中,我们使用了@Component、@Configuration以及带有@Bean注解的bean
我正在尝试将 spring、非启动应用程序迁移到启动应用程序。当前构建了一个 war 文件。正在关注these说明,我正在逐步完成迁移步骤。 我发现 @SpringBootApplication 注释
我对 SpringBoot 还很陌生。我创建了一个带有服务类的示例应用程序。 下面是我的SpringBootApplication类 @SpringBootApplication public cla
my earlier question 的答案说我应该使用 @SpringBootApplication(scanBasePackages = {"path.to.your.project.root"
我有一个 SpringBootApplication,我想从我的属性文件中读取一个值。 我的@SpringBootApplication类是这样的: @SpringBootApplication @C
如何使用@SpringBootApplication注释从类运行代码。我想运行我的代码而不调用 Controller 并从终端而不是网络浏览器获取信息。我尝试在@SpringBootApplicati
我在 Spring Boot 初始化时遇到了问题。我在一个简单的 Spring Boot 项目中有这个结构。 com.project.name |----App.java (Annoted with
我对 STS 很陌生。我在stackoverflow中搜索了这个错误。我没有找到正确的匹配答案。 我可以在 STS 的 MavenDependencies 部分中看到该类,但无法在我的 java 类中
有没有办法在运行mvn spring-boot:run时指定运行哪个SpringBootApplication的主类? docs说我可以使用 mainClass 参数来指定要运行的主类。但我不确定如何
我试过 @JdbcTest关于以下项目结构。 . ├── pom.xml └── src ├── main │ ├── java │ │ └── com │
我是一名优秀的程序员,十分优秀!