spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
1.01k stars 614 forks source link

[4.1.1] "java.lang.ClassCastException" when it is running on JUnit5 #2929

Closed SingKS8 closed 6 months ago

SingKS8 commented 7 months ago

Here is my demo project https://github.com/SingKS8/kafka-stream-demo . I have updated spring boot 3.2.4 and Spring cloud 2023.0.1 aka spring cloud stream 4.1.1 in my demo project which is a demo about spring cloud function, stream binding and kafka streams binder.

If I am running it as a application starting, like ./mvnw spring-boot:run; it is fine, there is no exception.

But if I am running the JUnit testcase I have got some class cast exceptions about "create producer binding" and "create consumer binding" like below.

2024-03-29T22:45:20.848+08:00 ERROR 78046 --- [kafka-stream-demo] [   scheduling-1] o.s.cloud.stream.binding.BindingService  : Failed to create producer binding; retrying in 30 seconds

java.lang.ClassCastException: class jdk.proxy2.$Proxy84 cannot be cast to class org.springframework.messaging.MessageChannel (jdk.proxy2.$Proxy84 is in module jdk.proxy2 of loader 'app'; org.springframework.messaging.MessageChannel is in unnamed module of loader 'app')
        at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindProducer(AbstractMessageChannelBinder.java:103) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.cloud.stream.binder.AbstractBinder.bindProducer(AbstractBinder.java:153) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.cloud.stream.binding.BindingService.lambda$rescheduleProducerBinding$4(BindingService.java:376) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-6.1.4.jar:6.1.4]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) ~[na:na]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) ~[na:na]
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

2024-03-29T22:45:20.850+08:00 ERROR 78046 --- [kafka-stream-demo] [   scheduling-1] o.s.cloud.stream.binding.BindingService  : Failed to create consumer binding; retrying in 30 seconds

java.lang.ClassCastException: class jdk.proxy2.$Proxy84 cannot be cast to class org.springframework.messaging.MessageChannel (jdk.proxy2.$Proxy84 is in module jdk.proxy2 of loader 'app'; org.springframework.messaging.MessageChannel is in unnamed module of loader 'app')
        at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.doBindConsumer(AbstractMessageChannelBinder.java:103) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.cloud.stream.binder.AbstractBinder.bindConsumer(AbstractBinder.java:144) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.cloud.stream.binding.BindingService.lambda$rescheduleConsumerBinding$1(BindingService.java:211) ~[spring-cloud-stream-4.1.1.jar:4.1.1]
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-6.1.4.jar:6.1.4]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) ~[na:na]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) ~[na:na]
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[na:na]
        at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]

I have downgraded the spring boot to 3.2.3 for testing, I still get those exceptions.

sobychacko commented 6 months ago

@SingKS8 Looks like this might be something related to your environment. I am able to run your test both on the IDE and on the command line using the following two commands:

./mvnw clean package
./mvnw clean package -Dtest=xyz.opcal.demo.KafkaStreamDemoApplicationTests

If you still think something is wrong, please provide specific instructions to reproduce the issue. Thanks!

SingKS8 commented 6 months ago

I have fix the demo docker compose config in testing, maybe my testcase function is just waiting the topic comsuming, it can not reflect the exceptions in bean initialization, then the test result is still success. So in my environment, both macos and linux desktop, JDK 21.0.3, in the test phase, I still got the exceptions above.

Because my testcase is useless, would you please check the log info, are there any consumer log like below in your test phase instead of exception.

2024-05-02T21:57:10.536+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [91-100]=12
2024-05-02T21:57:10.537+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [41-50]=18
2024-05-02T21:57:10.538+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [21-30]=10
2024-05-02T21:57:10.539+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [51-60]=17
2024-05-02T21:57:10.543+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [61-70]=13
2024-05-02T21:57:10.543+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [31-40]=16
2024-05-02T21:57:10.543+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [11-20]=22
2024-05-02T21:57:10.544+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [71-80]=6
2024-05-02T21:57:10.544+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [0-10]=20
2024-05-02T21:57:10.544+08:00  INFO 20008 --- [kafka-stream-demo] [-StreamThread-1] consumer                                 : **** [81-90]=16

My testing only got exceptions instead of those consumer log. If there are those logs in your test phase, I have to reset my environment. Thanks for your support!

sobychacko commented 6 months ago

Yes, I can see a lot of Kafka/Kafka Streams related consumer logs as above.

SingKS8 commented 6 months ago

Thanks for your support, I'll close this issue and review my project.