将Spring Boot从2.1.8升级到2.2.0后,未加载完整应用程序上下文的测试开始失败。
@DataJpaTest
@TestPropertySource(properties = "spring.jpa.hibernate.ddl-auto=none")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class MyRepositoryTest {
@Autowired private TestEntityManager entityManager;
@Autowired private MyRepository repository;
@Test
public void test() {
....
}
这会产生以下错误
20:44:35.866 [main] [ERROR] o.s.b.d.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method rabbitTemplate in com.example.boot.message.amqp.AmqpConfig required a bean of type 'org.springframework.amqp.rabbit.connection.ConnectionFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.amqp.rabbit.connection.ConnectionFactory' in your configuration.
我在@WebMvcTest上遇到了相同的错误,但之前都工作得很好。
在@RestController类上的注入失败,我认为不应为@DataJpaTest对其进行扫描和加载。
答案 0 :(得分:0)
问题的根本原因是应用程序类上的@ComponentScan批注,将@ComponentScan替换为@SpringBootApplication(scanBasePackages =“ com.example”)解决了该问题。