我正在使用 spring-cloud-stream-test-binder 版本 4.0.4 但是在发布/使用时我遇到了以下错误
java.lang.NullPointerException:无法调用“org.springframework.messaging.SubscribableChannel.send(org.springframework.messaging.Message)”,因为“org.springframework.cloud.stream.binder.test.InputDestination.getChannelByName”的返回值(字符串)”为空 在 org.springframework.cloud.stream.binder.test.InputDestination.send(InputDestination.java:89) 在 com.csn.tax.pfd.w2.evt.subscriber.service.ClientDataSubscriberTests.testExceptionOnGenerateW2Data(ClientDataSubscriberTests.java:512) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) 在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:568)
我的代码片段:
static final String INPUT_TOPIC_NAME = "tax-datafeed-evt-load-clt-dnr";
static final String OUTPUT_TOPIC_NAME = "tax-statusdata-evt-load-clt-dnr";
@Test
public void testExceptionOnGenerateW2Data() {
String fileId = "testFileId";
String loadId = "testLoadId";
String runId = "testRunId";
String branchNumber = "0080";
String clientAccountNumber = "INVALID_CLIENT";
String quarter = "3";
String year = "2020";
// Build Kafka event with INVALID_CLIENT
PayrollClientData payrollClientData = PayrollClientData.newBuilder()
.setFileId(fileId)
.setLoadId(loadId)
.setRunId(runId)
.setBranchNumber(branchNumber)
.setClientAccountId(clientAccountNumber)
.setQuarter(quarter)
.setYear(year)
.build();
Message<PayrollClientData> clientDataMessage = MessageBuilder.withPayload(payrollClientData).build();
// Send input message
inputDestination.send(clientDataMessage, INPUT_TOPIC_NAME);
// Receive output message
@SuppressWarnings("unchecked")
Message<PayrollLoadStatusData> payrollLoadStatusMessage = (Message<PayrollLoadStatusData>) (Object)
outputDestination.receive(500, OUTPUT_TOPIC_NAME);
PayrollLoadStatusData receivedPayrollLoadStatusData = payrollLoadStatusMessage.getPayload();
// Assert that received Kafka event contains the expected data for each field
assertNotNull(receivedPayrollLoadStatusData);
assertEquals(receivedPayrollLoadStatusData.getRunId().toString(), runId);
assertEquals(receivedPayrollLoadStatusData.getLoadId().toString(), loadId);
assertEquals(receivedPayrollLoadStatusData.getFileId().toString(), fileId);
assertEquals(receivedPayrollLoadStatusData.getQuarter().toString(), quarter);
assertEquals(receivedPayrollLoadStatusData.getYear().toString(), year);
assertEquals(receivedPayrollLoadStatusData.getClientAccountId().toString(), clientAccountNumber);
assertEquals(receivedPayrollLoadStatusData.getBranchNumber().toString(), branchNumber);
assertEquals(receivedPayrollLoadStatusData.getStatusType(), PayrollLoadStatusType.FAILED);
}
您发布的代码片段没有帮助,因为它没有显示有关测试如何设置的任何信息。 考虑查看我们使用 Test Binder 的一些测试作为示例 https://github.com/spring-cloud/spring-cloud-stream/blob/main/core/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/function /ImplicitFunctionBindingTests.java。 另外,您还可以看到一个孤立的样本here