SpringBoot之Junit单元测试

增加maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

HelloService.java

package com.zns.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {
       public String getName(){
              return"hello";
       }
}

HelloServiceTest.java

package com.zns.test;

import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.zns.MyApplication;
import com.zns.service.HelloService;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class HelloServiceTest {

    @Resource
    private HelloService helloService;

    @Test
    public void testGetName() {
        Assert.assertEquals("hello", helloService.getName());
    }
}

这时候就可以进行测试了,右键—RunAs – Junit Test。

猜你喜欢

转载自www.cnblogs.com/zengnansheng/p/10389795.html