Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:49:13+00:00 2026-05-27T20:49:13+00:00

Please, help me understand the following. I have an spring integration test, which I’m

  • 0

Please, help me understand the following.

I have an spring integration test, which I’m trying to test the method of ProcessCommentsDao class :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:testContext.xml"})
@Transactional()
public class ParseCommentsTest {

  @Resource
  private ProcessCommentsDao processCommentsDao;

  @Test
  public void testJMS() throws Exception {

     // Test data creation
     .........................

     processCommentsDao.parseComments();
   }
 }

In the method parseComments(), I get a list of entities, then each entity are processed through the Spring’s JMS MessageListener implementation:

@Service
public class ProcessCommentsDaoImpl extends BaseDao implements IProcessCommentsDao {

    private static final int PARSE_COMMENTS_COUNT_LIMIT = 100;
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Queue parseCommentsDestination;

    @Override
    public void parseComments() {

      List<Comment> comments = attributeDao.findTopUnparsedComments(PARSE_COMMENTS_COUNT_LIMIT);

      for (Comment comment : comments) {
        jmsTemplate.convertAndSend(parseCommentsDestination, comment);
      }
    }
}

Implementation of the MessageListener as follows:

@Component
public class QueueListener implements MessageListener {

  @PersistenceContext
  private  EntityManager em;

  @Transactional()
  public void onMessage(final Message message) {
      try {
         if (message instanceof ObjectMessage) {
            final ObjectMessage objMessage = (ObjectMessage) message;
            Comment comment = (Comment) objMessage.getObject();

            //...Some logic ...

             comment = em.find(Comment.class, comment.getId());
             comment.setPosStatus(ParsingType.PROCESSED);
             em.merge(comment);

              //...Some logic ...

    } catch (final JMSException e) {
        e.printStackTrace();
    }
}

}

As a result, the method em.find (Comment.class, comment.getId ()) returns null, because the data were created in another thread and the current thread does not know anything about this transaction. Is there a way to set up a transaction propagation so that MessageListener method seen entyties who were created in the main thread, in which the test method run?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:49:13+00:00Added an answer on May 27, 2026 at 8:49 pm

    As a result, the method em.find (Comment.class, comment.getId ()) returns null, because the data were created in another thread and the current thread does not know anything about this transaction.

    To be more exact, the message listener runs in a separate transaction, and it doesn’t see the data created by ParseCommentsTest.testJMS because that method did not commit.

    What’s more important, your test is not written correctly. It has a race condition: the calls to jmsTemplate.convertAndSend() are asynchronous, so the logic in QueueListener.messageListener() may be invoked after the test method completes (and rolls back the changes it has made). This test may yield different results each time it’s run.

    Your code is not easily testable, either. Consider extracting processing logic from the onMessage() method to a POJO and test it separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me understand if the following choices I have made are idiomatic/good and
I cannot understand how this is possible. Please help!! I have an app with
Please help! Background info I have a WPF application which accesses a SQL Server
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Could someone please help me to understand why the following block of code keeps
Please help me understand how the process goes. I understand that web browsers contain
Please help! I couldn't figure it out how to map the following situation: I
I have the following SQL statement which returns a single record as expected: select
I have the following classes: public class Folder{ private Set<Documents> documents; private Set<Clip> clips;
I have a following problem. I have a ListView which returns data from SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.