【问题标题】:MockWebServer BasicAuth with SpringMockWebServer BasicAuth 与 Spring
【发布时间】:2021-07-08 02:53:09
【问题描述】:

我需要通过调用外部 API 进行测试,我在其中执行以下操作:

  • 服务等级:
public class BatchStatusClient {

    @Value("${batchStatusUpdate.username}")
    private String username;

    @Value("${batchStatusUpdate.password}")
    private String password;

    public BatchStatusClient(@Value("${batchStatusUpdate.endpoint}")
            String urlBatchStatusUpdate) {
        this.webClient = WebClient.create("urlBatchStatusUpdate");
    }

    public Mono<JsonNode> getToken(String uuid) {
        return this.webClient.post()
                .uri("/authorization")
                .headers(httpHeaders1 -> {
                    httpHeaders1.setBasicAuth(username, password);
                    httpHeaders1.add(REQUEST_ALLOW_ORIGIN, "*");
                    httpHeaders1.add(REQUEST_ALLOW_CREDENTIALS, "false");
                    httpHeaders1.add(HttpHeaders.CONTENT_TYPE,
                            MediaType.APPLICATION_FORM_URLENCODED_VALUE);
                })
                .body(BodyInserters.fromFormData("grant_type", grantType))
                .retrieve()
                .bodyToMono(JsonNode.class);
    }
}

  • 测试类:
@TestPropertySource(locations = "classpath:/application-dev.yml")
@SpringBootTest()
@ExtendWith(SpringExtension.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = BatchStatusClient.class)
public class BatchStatusClientTest {
  
    private BatchStatusClient batchStatusClient;
    
    private static MockWebServer mockWebServer;
   
    @BeforeEach
    public void setup() throws IOException {
        mockWebServer = new MockWebServer();
        mockWebServer.start(9090);
        this.batchStatusClient = new BatchStatusClient(
                mockWebServer.url("/").toString());
    }

    @AfterAll
    static void tearDown() throws IOException {
        mockWebServer.shutdown();
    }
    
    @Test
    public void test_getToken() throws Exception {
        String jsonString = "{\"access_token\": \"QB7VGhGtQWEXB3J98lIjF3\"}";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jn = mapper.readTree(jsonString);
    
        mockWebServer.enqueue(new MockResponse()
                .setBody(new ObjectMapper().writeValueAsString(jn))
                .addHeader(HttpHeaders.CONTENT_TYPE,
                        MediaType.APPLICATION_JSON_VALUE));
        Mono<JsonNode> result = batchStatusClient.getToken("123");
        System.out.println("result = " + result);
        assertEquals("QB7VGhGtQWEXB3J98lIjF",
                result.block().get("access_token").asText());
    }
}

但是当我运行测试时出现以下异常:

java.lang.IllegalArgumentException: Username must not be null
  at org.springframework.util.Assert.notNull(Assert.java:201)
  at org.springframework.http.HttpHeaders.encodeBasicAuth(HttpHeaders.java:1834)
  at org.springframework.http.HttpHeaders.setBasicAuth(HttpHeaders.java:770)
  at org.springframework.http.HttpHeaders.setBasicAuth(HttpHeaders.java:751)

我应该做任何额外的配置吗?

【问题讨论】:

  • 您如何提供用户名值和密码值?
  • @Lakshman 在服务类中,像这样:@Value("${batchStatusUpdate.username}") private String username; @Value("${batchStatusUpdate.password}") 私有字符串密码;
  • 错误提示缺少对用户名字段的注入。确保 application-dev.yml 已读取并包含 batchStatusUpdate.username 的值。
  • @ChristianFrommeyer 没错,但我不明白为什么它不注入属性值,因为当我运行项目时它会注入。

标签: spring spring-boot basic-authentication mockwebserver


【解决方案1】:

这些修改对我有用:

BatchStatusClient 类:

public BatchStatusClient(@Value("${batchStatusUpdate.endpoint}")
                                 String urlBatchStatusUpdate,
@Value("${batchStatusUpdate.username}") String username,  
@Value("${batchStatusUpdate.password}") String password){
    this.webClient = WebClient.create(urlBatchStatusUpdate);
    this.username = username;
    this.password = password;

在设置方法中:

@BeforeEach
void setup() throws IOException {
    this.mockWebServer = new MockWebServer();
    this.batchStatusClient = new BatchStatusClient(
            mockWebServer.url("/").toString(), "username", "password"
    );
}

【讨论】:

    猜你喜欢
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多