SpringBoot如何进行代码测试

  1. 程序里面已经实现了一个最为简单的控制器程序类。

    package com.gwolf;

     

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.ResponseBody;

     

    @Controller

    @EnableAutoConfiguration

    public class SampleController {

     

        @RequestMapping("/")

        @ResponseBody

        public String index() {

            return "Greetings from Spring Boot!";

        }

        

        public static void main(String[] args) {

            SpringApplication.run(SampleController.class, args);

        }

    }

    SpringBoot如何进行代码测试

  2. 从实际的项目角度来讲,必须要求考虑到代码的测试问题,且现在的程序代码属于SpringBoot,则需要在你的项目之中进行如下的pom.xml文件的变更。

    【springbootbase】修改pom.xml配置文件,追加SpringBoot测试支持类:

    <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-test</artifactId>

                <scope>test</scope>

            </dependency>

            

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <scope>test</scope>

            </dependency>

    SpringBoot如何进行代码测试

  3. 只要进行java测试,最简单使用的就是junit,所以这个开发包一定要随测试一起导入。

    【springbootbase】建立一个测试程序类,现在是一个SpringBoot程序,所以需要使用注解:

    package com.gwolf.test;

     

    import com.gwolf.SampleController;

    import org.junit.runner.RunWith;

    import org.springframework.boot.test.context.SpringBootTest;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

     

    @SpringBootTest(classes = SampleController.class)

    @RunWith(SpringJUnit4ClassRunner.class)

    public class TestSampleController {

        

    }

    SpringBoot如何进行代码测试

  4. classes = SampleController.class表示我们需要进行单元测试的程序类:

    SpringBoot如何进行代码测试

  5. 因为我们需要测试的程序是web程序,所以还需要加入注解:@WebAppConfiguration。

     

    package com.gwolf.test;

     

    import com.gwolf.SampleController;

    import org.junit.runner.RunWith;

    import org.springframework.boot.test.context.SpringBootTest;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import org.springframework.test.context.web.WebAppConfiguration;

     

    @SpringBootTest(classes = SampleController.class)

    @RunWith(SpringJUnit4ClassRunner.class)

    @WebAppConfiguration

    public class TestSampleController {

        

    }

    SpringBoot如何进行代码测试

  6. 现在springboot整体测试程序如下:

    package com.gwolf.test;

     

    import com.gwolf.SampleController;

    import junit.framework.TestCase;

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.springframework.boot.test.context.SpringBootTest;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import org.springframework.test.context.web.WebAppConfiguration;

     

    import javax.annotation.Resource;

     

    @SpringBootTest(classes = SampleController.class)

    @RunWith(SpringJUnit4ClassRunner.class)

    @WebAppConfiguration

    public class TestSampleController {

        @Resource

        private SampleController sampleController;

        

        @Test

        public void testIndex() {

            TestCase.assertEquals(this.sampleController.index(),

                    "Greetings from Spring Boot!");

        }

    }

    SpringBoot如何进行代码测试

  7. 运行单元测试类,查看测试类是否有绿色的条。

    SpringBoot如何进行代码测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值