我可以使用 Spring 数据 JDBC 模拟 CrudRepository 而无需使用 Spring Boot 的任何数据库吗?
Can I mock CrudRepository with Spring Data JDBC without any database using Spring Boot?
我正在开发一个使用 Spring Boot 2 和 Spring Data JDBC 的应用程序。最终的应用程序将使用 MySQL.
我目前有一个持久层和一个服务层以及一些从 CrudRepository 派生的存储库。
对于我的单元测试,我真的想在没有任何数据库的情况下进行单元测试。甚至没有 HSQL。我知道这里有几种不同的哲学,但我想切换到纯单元测试,看看它是如何发挥作用的。我将在集成测试中使用数据库。
我的测试文件夹中有一个单独的 application.properties
,我在其中有一个单独的测试配置。
Spring 如果 application.properties
中未定义数据源,则引导将默认为 HSQL。但同样,我想在没有任何数据库的情况下 运行 我的测试,并且只模拟我的 CrudRepository 实现。我尝试通过在 application.properties
:
中使用以下不同的变体来禁用 Spring Boot 的数据库自动配置
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
在我的测试中,我试图做这样的事情:
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class UserServiceTest {
private UserService userService;
@MockBean
private UserRepository userRepository;
@BeforeEach
void setUp() {
userService = new UserService(userRepository);
}
@Test
public void doSomeTest() {
// Test code goes here
}
}
我在服务中使用基于构造函数的依赖注入。但是,我从 Spring 得到如下错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'package.userRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我想要的是一个模拟的 userRepository,我可以在其中预先配置一个伪造的存储库以 return 虚拟数据。但似乎删除数据库配置会完全删除 CrudRepository,因此 spring 启动应用程序的任何部分都无法启动。还是我漏掉了什么。
我正在使用 Eclipse、Maven、Mockito 和 JUnit5。我应该注意,如果我 运行 "unit tests" 针对测试 mysql 数据库,一切正常,所以我知道代码或测试或一般设置没有任何问题。只有当我想摆脱数据库时,问题才会出现。
我是初级 Java 开发人员,这样做是为了好玩,所以我可能遗漏了一些明显的东西。在网上搜索 Spring 数据 JDBC 很困难,因为我几乎只能找到与 JPA 相关的解决方案。
@SpringBootTest
加载您的所有上下文。 Spring boot – @SpringBootTest
Under the hood, @SpringBootTest tries to mimic the processes added by Spring Boot framework for creating the context e.g. it decides what to scan based on package structures, loads external configurations from predefined locations, optionally runs auto-configuration starters and so on.
As we see that this annotation starts and configure almost whole
application before the test begin, we should use @SpringBootTest to
write an integration tests that use the application processes and
dependencies.
包括UserService
。我猜它找不到 UserRepository
所以它失败了。
我猜你应该删除这些字符串
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
然后像这样测试
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class UserServiceTest {
@TestConfiguration
class UserServiceTestContextConfiguration {
@Bean
public UserService userService() {
return new UserService(userRepository);
}
}
@Autowired
private UserService userService;
@MockBean
private UserRepository userRepository;
// write test cases here
}
我拍了it from here
请记住 Spring 专门处理 @TestConfiguration
所以不要 "shot your leg" 使用它 Spring boot – @TestConfiguration
In spring boot, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. We must explicitly register the @TestConfiguration class with the class that contains the test cases.
我正在开发一个使用 Spring Boot 2 和 Spring Data JDBC 的应用程序。最终的应用程序将使用 MySQL.
我目前有一个持久层和一个服务层以及一些从 CrudRepository 派生的存储库。
对于我的单元测试,我真的想在没有任何数据库的情况下进行单元测试。甚至没有 HSQL。我知道这里有几种不同的哲学,但我想切换到纯单元测试,看看它是如何发挥作用的。我将在集成测试中使用数据库。
我的测试文件夹中有一个单独的 application.properties
,我在其中有一个单独的测试配置。
Spring 如果 application.properties
中未定义数据源,则引导将默认为 HSQL。但同样,我想在没有任何数据库的情况下 运行 我的测试,并且只模拟我的 CrudRepository 实现。我尝试通过在 application.properties
:
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
在我的测试中,我试图做这样的事情:
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class UserServiceTest {
private UserService userService;
@MockBean
private UserRepository userRepository;
@BeforeEach
void setUp() {
userService = new UserService(userRepository);
}
@Test
public void doSomeTest() {
// Test code goes here
}
}
我在服务中使用基于构造函数的依赖注入。但是,我从 Spring 得到如下错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'package.userRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我想要的是一个模拟的 userRepository,我可以在其中预先配置一个伪造的存储库以 return 虚拟数据。但似乎删除数据库配置会完全删除 CrudRepository,因此 spring 启动应用程序的任何部分都无法启动。还是我漏掉了什么。
我正在使用 Eclipse、Maven、Mockito 和 JUnit5。我应该注意,如果我 运行 "unit tests" 针对测试 mysql 数据库,一切正常,所以我知道代码或测试或一般设置没有任何问题。只有当我想摆脱数据库时,问题才会出现。
我是初级 Java 开发人员,这样做是为了好玩,所以我可能遗漏了一些明显的东西。在网上搜索 Spring 数据 JDBC 很困难,因为我几乎只能找到与 JPA 相关的解决方案。
@SpringBootTest
加载您的所有上下文。 Spring boot – @SpringBootTest
Under the hood, @SpringBootTest tries to mimic the processes added by Spring Boot framework for creating the context e.g. it decides what to scan based on package structures, loads external configurations from predefined locations, optionally runs auto-configuration starters and so on.
As we see that this annotation starts and configure almost whole application before the test begin, we should use @SpringBootTest to write an integration tests that use the application processes and dependencies.
包括UserService
。我猜它找不到 UserRepository
所以它失败了。
我猜你应该删除这些字符串
spring.autoconfigure.exclude= \
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration
然后像这样测试
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class UserServiceTest {
@TestConfiguration
class UserServiceTestContextConfiguration {
@Bean
public UserService userService() {
return new UserService(userRepository);
}
}
@Autowired
private UserService userService;
@MockBean
private UserRepository userRepository;
// write test cases here
}
我拍了it from here
请记住 Spring 专门处理 @TestConfiguration
所以不要 "shot your leg" 使用它 Spring boot – @TestConfiguration
In spring boot, any beans configured in a top-level class annotated with @TestConfiguration will not be picked up via component scanning. We must explicitly register the @TestConfiguration class with the class that contains the test cases.