我正在一个多模块项目中使用Spring Boot。鉴于我想测试隔离的每个模块,所以我创建了很多单元测试,现在我正在为我的服务项目编写集成测试。
也就是说,我创建了一个抽象配置,如下所示:
package br.com.rk.services;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* @author Rhuan Karlus
* @since 6/3/19
*/
@ExtendWith(SpringExtension.class)
@Sql({"/drop_data.sql", "/data.sql"})
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
properties = {
"spring.datasource.name=HikariCP-Pool",
"spring.datasource.type=com.zaxxer.hikari.HikariDataSource",
"spring.datasource.driver-class-name=org.hsqldb.jdbcDriver",
"spring.datasource.url=jdbc:hsqldb:mem:.",
"spring.datasource.username=sa",
"spring.datasource.password=",
"spring.flyway.enabled=true",
"spring.flyway.locations=classpath:/db/migrations/hsql",
"spring.data.jpa.repositories.enabled=true",
"spring.jpa.show-sql=true",
"debug=true"
})
public abstract class AbstractServiceIntegrationTest {
@SpringBootApplication(scanBasePackages = "br.com.rk")
static class TestApplication {
}
}
请注意,我的所有属性都在properties
批注的@SpringBootTest
参数中进行了描述。这样做是为了避免创建另一个application-test.properties
模块,因为它已经在api
模块上创建了(项目内部的另一个模块,它在服务层之上,因此对于服务层,它没有甚至不存在。)
这是一个简单的测试:
package br.com.rk.services;
import org.junit.jupiter.api.Test;
/**
* @author Rhuan Karlus
* @since 6/3/19
*/
public class TestandoIntegracao extends AbstractServiceIntegrationTest {
@Test
public void simpleTest() {
System.out.println("foo");
}
}
当我尝试运行simpleTest
方法时,会发生问题。它会正确加载除Bean之外的上下文配置,然后引发以下消息:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'br.com.rk.repositories.audit.AuditRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
... 84 more
如您所见,对数AuditRepository
表示没有合格的bean可以自动连接我的一项服务,但重点是该bean已定义并用@Repository
注释。并且此方法适用于api
模块(已经提到)。因此,我不了解这里的内容,也无法弄清楚如何配置这些测试以正确加载Spring Context以便访问所有bean。
只需写下来,这是我的AuditRepository
:
package br.com.rk.repositories.audit;
import br.com.rk.entities.audit.Audit;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* @author Rhuan Karlus
* @since 3/22/19
*/
@Repository
public interface AuditRepository extends JpaRepository<Audit, Long> {
}
-----------------------------编辑----------------- ------------
如果您想检查整个项目,请查看https://github.com/rhuankarlus/spring-boot-rest-api/tree/feature/%2315
我正在尝试测试project-services
模块。
答案 0 :(得分:1)
我并没有从头开始设置SpringBoot应用程序的经验。但是,我从未在测试类中使用package-private
作为SpringBoot应用程序类。我一直使用public
。试试:
@SpringBootApplication(scanBasePackages = "br.com.rk")
public class MyAwesomeApplication {
public static void main(String[] args) {
SpringApplication.run(MyAwesomeApplication .class, args);
}
}
这应该在src/main/java
目录中。
答案 1 :(得分:0)
代替
@SpringBootApplication(scanBasePackages = "br.com.rk.*")
尝试
@SpringBootApplication(scanBasePackages = "br.com.rk")