测试容器初始化后存储库始终为 null
repository always null after initilization of testing containers
我正在尝试使用 TestingContainers。我能够达到 运行 但我的测试始终为空。我试图避免嘲笑,而是拥有真实数据。
存储库
@Sql("classpath:data.sql")
class OrderDataRepositoryTest extends AbstractTestConfiguration {
//@Mock
@MockBean
//@Autowired
private OrderDataRepository orderRepository;
private AutoCloseable closeable;
@BeforeEach
public void init() {
closeable = MockitoAnnotations.openMocks(this);
}
@AfterEach
void closeService() throws Exception {
closeable.close();
}
@Test
void getAllUsersTest() {
List<Order> orders = orderRepository.findAll();
orders.toString();
}
}
配置
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
public abstract class AbstractTestConfiguration {
@Container
private MySQLContainer database = new MySQLContainer("mysql:8.0");
@Test
public void test() {
assertTrue(database.isRunning());
}
}
主要
@SpringBootTest
@Sql("classpath:init.sql")
@TestPropertySource("classpath:application-test.yml")
class TentingContainerApplicationTests {
}
application.properties
spring:
application:
datasource:
url: jdbc:mysql:8.0:///test?TC_INITSCRIPT=file:src/main/resources/init.sql
driver-class-name: com.mysql.jdbc.Driver
注释掉了
//@Mock
@MockBean
//@Autowired
是我试过的。模拟当然可行,但我想要@services 和@repository 类.
的真实数据
建议?
如果您想单独测试与数据库相关的代码(我假设您使用的是 Spring Data JPA),那么 @DataJpaTest
非常适合。
此注释将创建一个 sliced Spring context for you that contains only persistence relevant beans,例如:DataSource
、EntityManager
、YourRepository
。这不包括您的服务 类、您的 @Component
类 或 @RestController
.
默认情况下,此注释尝试将嵌入式内存数据库配置为 DataSource
。我们可以覆盖这个(你已经用你的一些代码示例做了)行为来使用 Testcontainers:
@DataJpaTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class OrderDataRepositoryTest {
@Container
static MySQLContainer database = new MySQLContainer("mysql:8.0");
@DynamicPropertySource
static void setDatasourceProperties(DynamicPropertyRegistry propertyRegistry) {
propertyRegistry.add("spring.datasource.url", database::getJdbcUrl);
propertyRegistry.add("spring.datasource.password", database::getPassword);
propertyRegistry.add("spring.datasource.username", database::getUsername);
}
@Autowired
private OrderDataRepository orderRepository;
@Test
void shouldReturnOrders() {
}
}
如果您想编写另一个包含所有 bean 并启动嵌入式 servlet 容器的测试,请使用 look at @SpringBootTest for writing integration tests。
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Testcontainers
class MyIntegrationTest {
@Container
static MySQLContainer database = new MySQLContainer("mysql:8.0");
@DynamicPropertySource
static void setDatasourceProperties(DynamicPropertyRegistry propertyRegistry) {
propertyRegistry.add("spring.datasource.url", database::getJdbcUrl);
propertyRegistry.add("spring.datasource.password", database::getPassword);
propertyRegistry.add("spring.datasource.username", database::getUsername);
}
@Autowired
private ServiceA serviceA;
@Autowired
private OrderDataRepository orderDataRepository;
}
在为测试和 Mockito 使用 Spring TestContext 时,请务必理解 difference between @Mock and @MockBean。
我正在尝试使用 TestingContainers。我能够达到 运行 但我的测试始终为空。我试图避免嘲笑,而是拥有真实数据。
存储库
@Sql("classpath:data.sql")
class OrderDataRepositoryTest extends AbstractTestConfiguration {
//@Mock
@MockBean
//@Autowired
private OrderDataRepository orderRepository;
private AutoCloseable closeable;
@BeforeEach
public void init() {
closeable = MockitoAnnotations.openMocks(this);
}
@AfterEach
void closeService() throws Exception {
closeable.close();
}
@Test
void getAllUsersTest() {
List<Order> orders = orderRepository.findAll();
orders.toString();
}
}
配置
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
public abstract class AbstractTestConfiguration {
@Container
private MySQLContainer database = new MySQLContainer("mysql:8.0");
@Test
public void test() {
assertTrue(database.isRunning());
}
}
主要
@SpringBootTest
@Sql("classpath:init.sql")
@TestPropertySource("classpath:application-test.yml")
class TentingContainerApplicationTests {
}
application.properties
spring:
application:
datasource:
url: jdbc:mysql:8.0:///test?TC_INITSCRIPT=file:src/main/resources/init.sql
driver-class-name: com.mysql.jdbc.Driver
注释掉了
//@Mock
@MockBean
//@Autowired
是我试过的。模拟当然可行,但我想要@services 和@repository 类.
的真实数据建议?
如果您想单独测试与数据库相关的代码(我假设您使用的是 Spring Data JPA),那么 @DataJpaTest
非常适合。
此注释将创建一个 sliced Spring context for you that contains only persistence relevant beans,例如:DataSource
、EntityManager
、YourRepository
。这不包括您的服务 类、您的 @Component
类 或 @RestController
.
默认情况下,此注释尝试将嵌入式内存数据库配置为 DataSource
。我们可以覆盖这个(你已经用你的一些代码示例做了)行为来使用 Testcontainers:
@DataJpaTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class OrderDataRepositoryTest {
@Container
static MySQLContainer database = new MySQLContainer("mysql:8.0");
@DynamicPropertySource
static void setDatasourceProperties(DynamicPropertyRegistry propertyRegistry) {
propertyRegistry.add("spring.datasource.url", database::getJdbcUrl);
propertyRegistry.add("spring.datasource.password", database::getPassword);
propertyRegistry.add("spring.datasource.username", database::getUsername);
}
@Autowired
private OrderDataRepository orderRepository;
@Test
void shouldReturnOrders() {
}
}
如果您想编写另一个包含所有 bean 并启动嵌入式 servlet 容器的测试,请使用 look at @SpringBootTest for writing integration tests。
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Testcontainers
class MyIntegrationTest {
@Container
static MySQLContainer database = new MySQLContainer("mysql:8.0");
@DynamicPropertySource
static void setDatasourceProperties(DynamicPropertyRegistry propertyRegistry) {
propertyRegistry.add("spring.datasource.url", database::getJdbcUrl);
propertyRegistry.add("spring.datasource.password", database::getPassword);
propertyRegistry.add("spring.datasource.username", database::getUsername);
}
@Autowired
private ServiceA serviceA;
@Autowired
private OrderDataRepository orderDataRepository;
}
在为测试和 Mockito 使用 Spring TestContext 时,请务必理解 difference between @Mock and @MockBean。