我正在使用WebTestClient编写JUnit测试来测试服务实现类(而不是控制器类),但我一直在遇到以下错误:-
*** 但是是:<404>
POST / authorizationserver / oauth / token WebTestClient-Request-Id:[1] 租户ID:[4370be48-6605-41da-89c5-dfc8945498ab] 相关ID:[4370be48-6605-41da-89c5-dfc8945498ab] 用户ID:[6e9dd5ef-0dbd-4a76-83aa-75cb85732851] 接受:[application / json; charset = UTF-8]
没有内容
<404未找到 <内容类型:[application / json; charset = UTF-8]
内容尚不可用
at com.sap.chatbot.commerceconnector.CommerceAuthenticationProviderTest.testAccessTokenWithHeaders(CommerceAuthenticationProviderTest.java:94)
由以下原因引起:java.lang.AssertionError:预期状态:<200>但为:<404> 在com.sap.chatbot.commerceconnector.CommerceAuthenticationProviderTest.testAccessTokenWithHeaders(CommerceAuthenticationProviderTest.java:94) ]]> ***
我的课程是:-
CommerceAuthenticationProvider.java
@Service("authenticationProvider")
public class CommerceAuthenticationProvider implements AuthenticationProvider {
private static final Logger logger = LoggerFactory.getLogger(CommerceAuthenticationProvider.class);
@Override
public AccessToken getUserAccessToken(Tuple2<String, WebClient> serviceConnectionDetails, MultiValueMap<String, String> queryParams) {
return serviceConnectionDetails._2
.post()
.uri(builder -> builder
.path(serviceConnectionDetails._1)
.queryParams(queryParams)
.build())
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
.retrieve()
.bodyToMono(AccessToken.class)
.block();
}
}
CommerceAuthenticationProviderTest.java:-
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = CommerceConnectorApplication.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"spring.main.webApplicationType=reactive"})
//@SpringBootTest()
@ActiveProfiles("test")
@AutoConfigureWebTestClient
public class CommerceAuthenticationProviderTest {
@Autowired
private WebTestClient webTestClient;
@MockBean
private AuthenticationProvider authenticationProvider;
public AccessToken getAccessToken() {
return new AccessToken("c5680a38-8ce2-4a28-93e4-db00fac4a2f9", "bearer", "39767", "basic openid");
}
public MultiValueMap<String, String> queryParams() {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
queryParams.add(CommerceConnectorConstants.AUTHORIZATION_CLIENT_ID, "asm");
queryParams.add(CommerceConnectorConstants.AUTHORIZATION_CLIENT_SECRET, "12341234");
queryParams.add(CommerceConnectorConstants.AUTHORIZATION_GRANT_TYPE,
CommerceConnectorConstants.REQUEST_HEADER_AUTHORIZATION_GRANT_TYPE);
queryParams.add(CommerceConnectorConstants.AUTHORIZATION_USERNAME, "abc@test.com");
return queryParams;
}
@Test
public void testAccessToken() throws Exception {
System.out.println("Test executed ->>>>>>>>>>>> "+webTestClient);
when(authenticationProvider.getUserAccessToken(any(), any())).then((res) -> Mono.just(getAccessToken()));
this.webTestClient.post().uri(builder -> builder.path("/authorizationserver/oauth/token")
.queryParams(queryParams())
.build())
.header(CommerceConnectorConstants.TENANT_ID, "4370be48-6605-41da-89c5-dfc8945498ab")
.header(CommerceConnectorConstants.CORRELATION_ID, "4370be48-6605-41da-89c5-dfc8945498ab")
.header(CommerceConnectorConstants.TENANT_USER_ID, "6e9dd5ef-0dbd-4a76-83aa-75cb85732851")
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus()
.isOk();
}
}
POM.xml(代码块):-
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我尝试了所有方法,但无法找出问题所在。 请指教。谢谢