qq_16237125 2021-11-11 09:27 采纳率: 60%
浏览 32
已结题

Springboot, Mybatis, Junit4 , Pagehelper 单元测试不起作用

application.properties



mybatis.type-aliases-package=com.greatschools.models
#mybatis.type-handlers-package=com.greatschools.models.typehandler
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30

mybatis.mapper-locations=classpath:mapping/*.xml
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:8883/schools
spring.datasource.username=root
spring.datasource.password=Infosys123

pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.pageSizeZero=false

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.greatschools.models</groupId>
    <artifactId>greatschools-models</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>greatschools-models</name>
    <description>Model project for school domain</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>


        <!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> 
            <version>2.2.0</version> </dependency> -->

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.2.0</version>
         
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.0</version>
        </dependency>

 


        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> 
            <version>2.5.0</version> </dependency> -->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

junit 代码

package com.greatschools.models;

import junit.framework.TestCase;
import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 

@RunWith(SpringRunner.class)
@MybatisTest
@Slf4j
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class SchoolTest {
    //@Resource
    @Autowired
    private SchoolMapper mapper;

    private static final Logger logger = LogManager.getLogger();
    /**
     * 
     */
    @Test
    public void TestSelectById() {
        logger.debug("TestSelectById");
        
        School s = mapper.selectByPrimaryKey(5270);
        TestCase.assertEquals((Integer) 5270, s.getId());;
    }
    
    /**
     * 
     */
    @Test
    public void TestSelectByLocalityId() {
        logger.debug("TestSelectByLocalityId");

        List<School> schools = mapper.selectByLocalityId("310115" );
        System.out.print(schools.size());
        TestCase.assertTrue(schools.size()>0);
    }
    
    /**
     * 
     */
    @Test
    public void TestSelectByLocalityIdWithPage() {
        logger.debug("TestSelectByLocalityId");
        PageHelper.startPage(1, 10);
        List<School> schools = mapper.selectByLocalityId("310115" );
        
        PageInfo<School> pageInfo = new PageInfo<School>( schools);
        TestCase.assertEquals(10,pageInfo.getList().size());
    }
}
  • 写回答

2条回答 默认 最新

  • I'Msohs 2021-11-11 09:34
    关注

    分页查询的话用mybatis-plus,比这个简单

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月21日
  • 已采纳回答 11月13日
  • 修改了问题 11月11日
  • 创建了问题 11月11日

悬赏问题

  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错