I have Spring boot with RabbitMQ back-end application. I want to run my JUnit test case with JMeter for load testing. While I run Junit test case , I face below error. OmniRoute class available from other jar file. When I run Junit test case from my IDE, then it is running successfully but when I run it from JMeter then I am facing below error. I have shared my code as well.
Error -- testShrotUrlGenerate(org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): mb/omni/proto/OmniRoute Trace -- java.lang.NoClassDefFoundError: mb/omni/proto/OmniRoute at mb.omni.shorturl.handlers.ShortUrlGenerationHandlerTest.testShrotUrlGenerate(ShortUrlGenerationHandlerTest.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase.runTest(JUnitSampler.java:596) at junit.framework.TestResult.runProtected(TestResult.java:142) at org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:396) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) at java.lang.Thread.run(Unknown Source) Error -- testShrotUrlGenerate(org.apache.jmeter.protocol.java.sampler.JUnitSampler$AnnotatedTestCase): org/springframework/amqp/rabbit/core/RabbitTemplate Trace -- java.lang.NoClassDefFoundError: org/springframework/amqp/rabbit/core/RabbitTemplate at mb.omni.shorturl.handlers.ShortUrlGenerationHandlerTest.cleanup(ShortUrlGenerationHandlerTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.jmeter.protocol.java.sampler.JUnitSampler.sample(JUnitSampler.java:400) at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:627) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:551) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:490) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:257) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.springframework.amqp.rabbit.core.RabbitTemplate at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 11 more
I am generating jar file of project with below gradle file.
jar {
enabled=true
manifest {
attributes "Main-Class": "MainClassName"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Below is my configuration class with RabbitMQ.
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class ShortUrlGenerateConfig {
@Bean
public DirectExchange shortUrlExchange() {
return new DirectExchange("exchange", true, false);
}
@Bean
public Queue shortUrlGenerateQueue() {
return new Queue("queue");
}
@Bean
public Binding shortUrlGenerateBinding() {
return BindingBuilder.bind(shortUrlGenerateQueue()).to(shortUrlExchange()).with("routingKey");
}
@Bean
public ShortUrlGenerationHandler shortUrlGenerationHandler() {
return new ShortUrlGenerationHandler();
}
}
This is my handler class.
public class ShortUrlGenerationHandler {
private static final Logger logger = LoggerFactory.getLogger(ShortUrlGenerationHandler.class);
@Autowired
private RabbitTemplate rabTmp;
/**
* @param message
* generate shorturl
*/
@RabbitListener(queues = "queue")
public void consumeMessageFromGenerateQueue(final Message<?> message) {
String routingKey = (String) message.getHeaders().get(AmqpHeaders.RECEIVED_ROUTING_KEY);
logger.info("ShortURL Message received on Queue["+message.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE)+"], Routing Key["+routingKey+"].");
Object correlationId = message.getHeaders().get(AmqpHeaders.CORRELATION_ID);
Object directReplyTo = message.getHeaders().get(AmqpHeaders.REPLY_TO);
ShortURLRequest request = null;
ShortURLRequest.Builder response = null;
try {
request = ShortURLRequest.parseFrom((byte[]) message.getPayload());
response = ShortURLRequest.newBuilder();
if (correlationId == null || directReplyTo == null) {
throw new ResourceNotFoundException("CORRELATION_ID_NOT_FOUND","Invalid Correlation ID, Correlation ID not found",9103);
}
.
.
.
}catch (Exception e) {
logger.error(" Error in consumeMessageFromGenerateQueue method.Error is - " + e.getMessage(), e);
}
logger.info(" response - " + response);
MessageProperties props = MessagePropertiesBuilder.newInstance().setCorrelationId(correlationId.toString())
.build();
rabTmp.send("", directReplyTo.toString(),
MessageBuilder.withBody(response.build().toByteArray()).andProperties(props).build());
}
}
Below is my Junit test case.
@Test
public void testShrotUrlGenerate() throws Exception {
OmniRoute omniRoute = OmniRoute.newBuilder()
.setRouteType("AMQP")
.setProcessor("processor")
.setAmqpRoute(OmniAmqpRoute.newBuilder()
.setRoutingkey("routingKey")
.setExchange("exchange")
.build())
.build();
List<OmniRoute> omniRoutes = new ArrayList<>();
omniRoutes.add(omniRoute);
OmniRequest omniRequest = OmniRequest.newBuilder()
.setRequestId("182fd990725011e9a9ca8f4121c06f8b12")
.setRequestType("SHORT_URL")
.addAllRoutes(omniRoutes)
.setUserId(100)
.build();
ShortURL shortUrl = ShortURL.newBuilder().setOriginalUrl("http://dashboard.omni.platform/#/login").setUser(User.newBuilder()
.setUserId(100))
.setMessageId("3")
.setChannel("CHANNEL_ONE")
.build();
ShortURLRequest shortURLRequest = ShortURLRequest.newBuilder().setRequest(omniRequest).addShortUrls(shortUrl).build();
MessageProperties messageProperties = new MessageProperties();
messageProperties.setCorrelationId("1f4a13d0-f3b4-4b4b-98b4-50e9d75d6032");
Message message = new Message(shortURLRequest.toByteArray(), messageProperties);
byte[] responseByte = (byte[]) template.convertSendAndReceive(shortUrlExchange, shortUrlGenerateRoutingKey, message);
ShortURLRequest shortURLResponse = ShortURLRequest.parseFrom(responseByte);
assertTrue(shortURLResponse.getShortUrlsList().get(0).getShortUrlId()>0);
assertEquals(shortURLResponse.getShortUrlsList().get(0).getOriginalUrl(),shortUrl.getOriginalUrl());
assertTrue(shortURLResponse.getShortUrlsList().get(0).getShortUrlCode()!= null);
assertEquals(shortURLResponse.getShortUrlsList().get(0).getUser().getUserId(),shortUrl.getUser().getUserId());
}