【问题标题】:repository always null after initilization of testing containers初始化测试容器后存储库始终为空
【发布时间】: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 类的真实数据。

建议?

【问题讨论】:

    标签: mockito testcontainers


    【解决方案1】:

    如果您想单独测试与数据库相关的代码(我假设您使用的是 Spring Data JPA),那么@DataJpaTest 非常适合。

    此注解将创建一个sliced Spring context for you that contains only persistence relevant beans,例如:DataSourceEntityManagerYourRepository。这不包括您的服务类、@Component 类或 @RestController

    默认情况下,此注解尝试将嵌入式内存数据库配置为DataSource。我们可以覆盖这个(你已经用你的一些代码示例做了)行为来使用测试容器:

    @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-23
      • 2021-05-19
      • 2022-11-08
      • 2023-02-08
      • 1970-01-01
      • 2019-06-13
      • 2014-03-19
      • 1970-01-01
      相关资源
      最近更新 更多