springboot单元测试及其组件

一、自带的单元测试

@SpringBootTest
class AdmindemoApplicationTests {

    @Autowired
    private AdminService as;
    @Test
    void contextLoads() {
        //测试的应用程序 未启用springboot服务
        System.out.println(as.selectOne("a1"));
    }

}

二、postman工具

三、lombok插件--用注解实现get、set等的创建

 

安装之后重启idea

用lombok简化实体类

@Data注解:编译的时候,给class,提供getset方法

主键自增:添加注解@TableId  plus提供

 四、Mybatis-plus

dao和service方法对比
service中默认有baseMapper属性,即是自动注入的Dao,无需再额外注入,可以再添加其它实体类的Dao
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
    protected Log log = LogFactory.getLog(this.getClass());
    @Autowired
    protected M baseMapper;
    ...
dao主要方法如下:insert、delete、update、select...
public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int delete(@Param("ew") Wrapper<T> wrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);
    
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}
service主要方法如下:save、remove、update、get、list、page
public interface IService<T> {
//插入
    default boolean save(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().insert(entity));
}
//删除
    default boolean removeById(Serializable id) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
}
//按条件删除
    default boolean remove(Wrapper<T> queryWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().delete(queryWrapper));
}
//批量删除
    default boolean removeByIds(Collection<? extends Serializable> idList) {
        return CollectionUtils.isEmpty(idList) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(idList));
}
//更新
    default boolean updateById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
    }    
    default boolean update(Wrapper<T> updateWrapper) {
        return this.update((Object)null, updateWrapper);
}    
//按条件进行修改
    default boolean update(T entity, Wrapper<T> updateWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
}
//selectOne
    default T getById(Serializable id) {
        return this.getBaseMapper().selectById(id);
}
//总数
    default int count() {
        return this.count(Wrappers.emptyWrapper());
}    
//按条件查询总数
    default int count(Wrapper<T> queryWrapper) {
        return SqlHelper.retCount(this.getBaseMapper().selectCount(queryWrapper));
}    
//按条件查询
    default List<T> list(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectList(queryWrapper);
}    
//selectList
    default List<T> list() {
        return this.list(Wrappers.emptyWrapper());
}
//分页 有条件(可以加入条件查询)
    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectPage(page, queryWrapper);
}    
//分页
    default <E extends IPage<T>> E page(E page) {
        return this.page(page, Wrappers.emptyWrapper());
    }
}

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.sxl</groupId>
    <artifactId>plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>plus</name>
    <description>Demo project for Spring Boot</description>
    <!--<properties>
        <java.version>1.8</java.version>
    </properties>-->
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.8.RELEASE</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 配置mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
        <!-- 配置mybatis-plus生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.0</version>
            <!-- 当前依赖不向下传递 -->
            <optional>true</optional>
        </dependency>
        <!-- 配置mybatis-plus生成器默认模板 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
            <optional>true</optional>
        </dependency>
        <!-- 配置mybatis-plus分页插件 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.sxl.PlusApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 单元测试启动不了项目可能是由多种原因引起的,因为这涉及到Spring Boot应用的生命周期和测试环境的配置。以下是一些常见的问题及其可能的解决方案: 1. **缺少测试类**:确保你的测试类继承了`SpringBootTest`或相关的基类,并且包含一个公共的无参构造器。 2. **依赖注入问题**:如果测试类没有正确地扫描到或配置了所需的bean,那么这些bean在测试中可能无法被自动创建。检查是否有使用`@Autowired`注解并确认所有需要的组件都被`@ComponentScan`正确扫描。 3. **启动类问题**:确保你的`@SpringBootApplication`类在测试环境下能正常初始化。有些情况下,你可能需要使用`@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)`来隔离web部分。 4. **测试资源路径**:如果你的测试依赖于配置文件或其他资源文件,确保它们被正确地定位到测试类查找的路径。 5. **模块化问题**:如果你的应用是模块化的,确保测试和生产代码都在相同的模块或正确的依赖结构下。 6. **测试环境设置**:Spring Boot默认在测试环境中禁用一些特性,如AOP、缓存等,检查`@EnableAutoConfiguration(exclude = ...)`是否排除了必要的配置。 7. **IDE配置**:确保IDE的测试运行配置和你的项目设置一致,例如Maven或Gradle插件的配置。 8. **错误日志**:检查测试运行过程中是否有任何异常输出,这有助于定位问题。 如果你已经排查过以上可能,仍然无法解决问题,你可以提供具体的错误信息,比如堆栈跟踪,这样我可以更准确地帮助你。相关问题如下: 1. 测试类是如何定义的? 2. 你在测试中尝试创建哪些bean,有没有抛出异常? 3. 你的`@SpringBootTest`或其他启动类的配置有何特殊之处?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值