---
title: "Spring boot junit test"
author: "qingfeng825"
date: "2020-06-16T16:06:47Z"
tags: []
categories: ["开源软件"]
slug: "spring-boot-junit-test"
draft: false
lastmod: "2020-06-16T16:06:47Z"
id: 2514902
url: "https://qingfeng825.iteye.com/blog/2514902"
---

1\. 下面的AgreementService 引用了 FeignClient Service, 因为不想真正的调用依赖的Service，所以建了1.3. 想使用Stub代替FeignClient.

 

1.1 AgreementService Test

 

```
@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class AgreementServiceTest {


	@Autowired
	private AgreementService agreementService;
	

	@Test
	public void testListAgreementsByCustNumber() {
		String customerNumber = "XXXX";
		List<AgreementBo> agreementBos = agreementService.listAgreementsByCustNumber(customerNumber);
		System.out.println(agreementBos);
		//assertTrue(CollectionUtils.isNotEmpty(agreementBos));
	}

}
```

 1.2 Agreement feign client (src)

 

 

```
public interface AgreementClient {

	/**
	 * Search agreement by customer number.
	 *
	 * @param customerNumber the customer number
	 * @return the api result
	 */
	@RequestMapping(value = "/{customerNumber}/agreements", method = RequestMethod.GET)
	public ApiResult<List<AgreementDto>> searchAgreementsByCustomerNumber(
			@PathVariable("customerNumber") String customerNumber);
}
```

 1.3 Feign client stub in Test

```
@Component
@Primary
public class AgreementFeignClient implements AgreementClient {

	@Override
	public ApiResult<List<AgreementDto>> searchAgreementsByCustomerNumber(String customerNumber) {
		List<AgreementDto> agreementDtoList = new ArrayList<AgreementDto>();
		try {}
                 return 
}
}
```

 发生的问题：

more than one 'primary' bean found among candidates: \[agreementFeignClient, com.ibm.dsw.customer.feignclient.agreement.AgreementClient]

Solution:

```
primary = false
```

```
@FeignClient(url = "${}", path = "/v1/xx", name = "xx-xx-service", configuration = FeignInterceptor.class,primary = false)
public interface AgreementClient {}
```