使用Spock和groovy、SpringBoot测试我的基于注解的myBatisMap器

mw3dktmi  于 2022-11-23  发布在  Spring
关注(0)|答案(2)|浏览(214)

我基于注解创建了myBatisMap器,如以下 * 更改代码 * 示例所示:

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MyTableMapper {

    @Insert("INSERT INTO MY_TABLE(" +
                "Column_1, " +
                "Column_2 " +
            ") VALUES (" +
                "#{myTableModel.columnOne}, " +
                "#{myTableModel.columnTwo} " +
            ")")
    int insert(MyTableModel myTableModel);

    @Select("SELECT * FROM MY_TABLE")
    @Results(value = {
            @Result(id = true, property = "columnOne", column = "Column_1"),
            @Result(property = "columnTwo", column = "Column_2")
    })
    List<MyTableModel> findAll();

    @ResultType(Integer.class)
    @Select("SELECT COUNT(*) FROM MY_TABLE " +
            "WHERE Column_1 = #{columnOne} AND Column_2 = #{columnTwo}")
    int countByColumnOneAndColumnTwo(@Param("columnOne") String columnOne, @Param("columnTwo") String columnTwo);

    @Update("UPDATE MY_TABLE SET " +
            "Column_3 = #{myTableModel.columnThree}, "
            "Column_4 = #{myTableModel.columnFour} " +
            "WHERE Column_1 = #{myTableModel.columnOne} AND Column_2 = #{myTableModel.columnTwo}")
    int updateByColumnOneAndColumnTwo(@Param("myTableModel") MyTableModel myTableModel);

    @Delete("DELETE FROM MY_TABLE " +
            "WHERE Column_1 = #{myTableModel.columnOne} AND Column_2 = #{myTableModel.columnTwo}")
    void deleteByColumnOneAndColumnTwo(@Param("myTableModel") MyTableModel myTableModel);

}

注意事项!我正在使用Spring Cloud ConfigHashicorp Vault

spring:
  datasource:
    url: jdbc:sqlserver://172.xx.yy.zzz:1433;databaseName=myDatabase;encrypt=true;trustServerCertificate=true;
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    testWhileIdle: true
    testOnBorrow: true
    hikari:
      connection-test-query: SELECT 1
      ....
  • 某些yaml配置文件 *
#Hashicorp Vault
spring.cloud.vault:
  scheme: ${VAULT_SCHEME:http}
  host: ${VAULT_HOST:localhost}
  port: ${VAULT_PORT:8xyz0}
  ...
  application-name: my-microservice-name

#Config Server
spring.cloud.config:
  ...
  uri: ${SPRING_CLOUD_CONFIG_URI:http://localhost:8xx8}

凭据存储在保管库中(* 要创建SqlSession,XML文件中的其他内容不是选项*)
现在我想测试我的Map器。我使用@MybatisTest* 部分找到了这个documentation *。
我的测试Spock文件!

import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification

class MyTableMapperSpec extends Specification {

    @Autowired
    private MyTableMapper myTableMapper
    private MyTableModel myTableModel

    void setup() {
        def myTableModelId = MyTableModelId.builder()
              //.someFields
                .build()
        myTableModel = MyTableModel.builder()
              //.someFields
                .build()
        myTableMapper.deleteByColumnOneAndColumnTwo(myTableModel)
        myTableMapper.insert(myTableModel)
    }

    def "FindAll"() {
        when:
        List<MyTableModel> listMyTableModel = myTableMapper.findAll()
        then:
        !listMyTableModel.isEmpty()
    }

    void cleanup() {
        //myTableMapper.deleteByColumnOneAndColumnTwo(myTableMapper)
    }
}

当我这样试探这句台词时:myTableMapper.deleteByColumnOneAndColumnTwo(myTableModel)我注意到我的**myTableMapper**是null!也许@Autowired注解不适合我的代码!

如何解决此问题(获取Map器的实现)?

lmvvr0a8

lmvvr0a81#

文件包括:

import org.mybatis.spring.boot.test.autoconfigure.MybatisTest

@RunWith(SpringRunner.class)
@MybatisTest
public class CityMapperTest {

    @Autowired
    private CityMapper cityMapper;

    @Test
    public void findByStateTest() {
        City city = cityMapper.findByState("CA");
    }

}

build.gradle

implementation group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-test-autoconfigure', version: 'x.y.z'

但是,@MybatisTest将失败,因为您没有dataSourcesqlSessionFactory或其他!
尝试使用:@SpringBootTest

@SpringBootTest(classes = YourApplication.class) /* your annotated class with @SpringBootApplication */
class MyTableMapperSpec extends Specification {
 ...
}

@SpringBootTest
class MyTableMapperSpec extends Specification {
 ...
}
bvpmtnay

bvpmtnay2#

那么,您实际上需要用@MybatisTest来注解Spock规范。除了spock-core依赖项之外,您还需要spock-spring依赖项。

相关问题