Skip to content

Commit

Permalink
微调配置注解的位置,将@EnableFeignClients挪到FeignConfig。因为将@EnableFeignClients放在@…
Browse files Browse the repository at this point in the history
…SpringBootApplication上会导致使用@WebMvcTest的测试失败

spring-projects/spring-boot#7270
  • Loading branch information
iuwjh committed Sep 11, 2020
1 parent 36e3f7b commit f351036
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallAuthServerApplication.class, GulimallCommonModule.class})
public class GulimallAuthServerApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallCartApplication.class, GulimallCommonModule.class},exclude = DataSourceAutoConfiguration.class)
public class GulimallCartApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.atguigu.common.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
@EnableFeignClients
@Configuration
public class FeignConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.atguigu.common.config;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;

@EnableDiscoveryClient
@Configuration
public class NacosConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.atguigu.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestClientConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.atguigu.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.api.Assertions;
import org.springframework.test.web.servlet.ResultMatcher;

public class ResponseBodyMatchers {
private final ObjectMapper objectMapper = new ObjectMapper();

public <T> ResultMatcher containsObjectAsJson(
Object expectedObject, Class<T> targetClass) {
return mvcResult -> {
String json = mvcResult.getResponse().getContentAsString();
T actualObject = objectMapper.readValue(json, targetClass);
Assertions.assertThat(actualObject).isEqualToComparingFieldByField(expectedObject);
};
}

public static ResponseBodyMatchers responseBody() {
return new ResponseBodyMatchers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.atguigu.common;

import com.atguigu.common.utils.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.api.Assertions;
import org.springframework.test.web.servlet.ResultMatcher;

public class Rmatcher {
private final ObjectMapper objectMapper = new ObjectMapper();

public <T> ResultMatcher hasKeyValue(String key, T val, Class<T> valClass) {
return mvcResult -> {
String json = mvcResult.getResponse().getContentAsString();
R r = objectMapper.readValue(json, R.class);
Assertions.assertThat(r.get(key)).isInstanceOf(valClass);
Assertions.assertThat(r.get(key)).isEqualToComparingFieldByField(val);
};
}


public static Rmatcher Rm() {
return new Rmatcher();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.atguigu.common;

import org.springframework.context.annotation.Configuration;

@Configuration
public class WebMvcTestConfig {

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
*
*
*/
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallCouponApplication.class, GulimallCommonModule.class})
public class GulimallCouponApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* 2、编写网关配置文件
*/

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallGatewayApplication.class, GulimallCommonModule.class},exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* 1、声明接口的每一个方法都是调用哪个远程服务的那个请求
* 3)、开启远程调用功能
*/
@EnableRedisHttpSession
@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallMemberApplication.class, GulimallCommonModule.class})
public class GulimallMemberApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy(exposeProxy = true)
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallOrderApplication.class, GulimallCommonModule.class})
public class GulimallOrderApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
* 1)、编写异常处理类,使用@ControllerAdvice。
* 2)、使用@ExceptionHandler标注方法可以处理的异常。
*/
@EnableFeignClients(basePackages = "com.atguigu.gulimall.product.feign")
@EnableDiscoveryClient
@MapperScan("com.atguigu.gulimall.product.dao")
@SpringBootApplication(scanBasePackageClasses = {GulimallProductApplication.class, GulimallCommonModule.class})
public class GulimallProductApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableFeignClients(basePackages = "com.atguigu.gulimall.search.feign")
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallSearchApplication.class, GulimallCommonModule.class}, exclude = DataSourceAutoConfiguration.class)
public class GulimallSearchApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallSeckillApplication.class, GulimallCommonModule.class},exclude = DataSourceAutoConfiguration.class)
public class GulimallSeckillApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ComponentScan;

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallThirdPartyApplication.class, GulimallCommonModule.class})
public class GulimallThirdPartyApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@



@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackageClasses = {GulimallWareApplication.class, GulimallCommonModule.class})
public class GulimallWareApplication {

Expand Down

0 comments on commit f351036

Please sign in to comment.