1
votes

I have the following unit test that gives me a null pointer exception. The debugger goes to the finally block right after the line that creates a connection factory. Here's the test:

@Test
    public void receiveMessage() throws Exception {
            MessageConsumer consumer = null;
            ConnectionFactory connectionFactory = null;
        Connection connection = null;
        Session session = null;
        Destination destination = null;
            try {
                   connectionFactory = new ActiveMQConnectionFactory(
                        "tcp://localhost:61616");
                 connection = connectionFactory.createConnection("username","pwd");
                 connection.start();
                 session = connection.createSession(true,
                        Session.AUTO_ACKNOWLEDGE);

                 destination = session
                                    .createQueue("myQueue");
                    logger.info("Starting consumer");
                    consumer = session
                                    .createConsumer(destination);

                    Message m = consumer.receive(); 
                    logger.info("Received message:" + m); 
            }


            } catch (Exception e) {
                    logger.error("", e);
            } finally {
                    consumer.close();
                    session.close();
                    connection.stop();
                    connection.close();
            }
    }

Here's the stack trace:

java.lang.NullPointerException at ClassName.receiveMessage(ClassName.java:62) 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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192

)

1

1 Answers

0
votes

In Unit testing actual connections should not be used, i.e you are trying to connect to BrokerURL in unit test case, which is not what unit testing is meant for.

In such cases you need to mock the external connections,using libraries such as Easymock+Powermock or Mockito

and regarding the null pointer exception,is your broker up? and username,password correct,please check these