Home > OS >  Testing my annotation based myBatis mapper, using Spock and groovy, SpringBoot
Testing my annotation based myBatis mapper, using Spock and groovy, SpringBoot

Time:11-18

I created myBatis mapper based on annotations as this changed code example:

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);

}

CONSIDERATIONS! I'm using Spring Cloud Config and Hashicorp 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
      ....

Some yaml config file

  #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}

The credentials are stored in vault (To create SqlSession, and other things in XML file are not option)

Now I would like to test my mapper. I found this documentation Using @MybatisTest part.

My Test Spock File!

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)
    }
}

When I like to test this line: myTableMapper.deleteByColumnOneAndColumnTwo(myTableModel) I noted that my myTableMapper is null! Maybe the @Autowired annotation is not suitable in my code!

How solve this (Obtain an implementation of my mapper)?

CodePudding user response:

Well, you then need actually annotate your Spock specification with @MybatisTest. You also need the spock-spring dependency in addition to the spock-core dependency.

CodePudding user response:

The documentation has:

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");
    }

}

The build.gradle

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

But, @MybatisTest will fail because you don't have dataSource, sqlSessionFactory or something!

Try with: @SpringBootTest

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

or

@SpringBootTest
class MyTableMapperSpec extends Specification {
 ...
}
  • Related