SpringBoot Test集成测试(service层)

如何测试SpringBoot的请求?使用spring-boot-starter-test这个包即可完成测试

关键点:

  • 引入spring boot依赖
  • 加@SpringBootTest(classes = Application.class) @RunWith(SpringJUnit4ClassRunner.class)注解
  • 加上@before @after
  • 注入自己需要bean

pom.xml



    org.springframework.boot
    spring-boot-starter-test
    2.0.3.RELEASE
    test


    org.mockito
    mockito-all
    1.9.5
    test

    junit
    junit
    4.12
    test

注:此处的spring-boot-starter-test主要是导入mockito-core的jar包,关于mockito了解详细可查看:http://www.cnblogs.com/zishi/p/6780719.html

package cn.yunpay.account.service.impl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.yunpay.account.service.AccountServiceBootStart;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=AccountServiceBootStart.class)
@EnableAutoConfiguration
public class AccountBasicServiceImplTest {

	@Autowired
	AccountBasicServiceImpl accountBasicServiceImpl;
	
	@Test
	public void testAddBaseAccount() {
		accountBasicServiceImpl.addBaseAccount("", "");
	}

}




你可能感兴趣的:(maven,SpringBoot,测试)