【发布时间】:2021-12-06 17:38:22
【问题描述】:
我正在尝试使用 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 类的真实数据。
建议?
【问题讨论】: