Announcement Announcement Module
No announcement yet.
JUnit testcase stops without waiting for data from database Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit testcase stops without waiting for data from database

    Hi all,
    I am trying run the test case for my application, in which I load the app-context.xml.
    app-context.xml connects to 3 databases, fetches data, put it in a channel, then there is a transformer which extracts the data out of it and creates a string which is passed to int-file:outbound-channel-adapter.

    My application is running fine when run as main. But when it is run using the JUnit testcase it stops after loading the context and does not wait for the database to return the result and my testcase fails.

    But if I puts a Thread.sleep(30000) just after loading the context then it gets time to fetch the data from the database and generate the file. And then my testcase works fine.

    So, I need a better solution and want to remove this Thread.sleep().

    Please help.

    Thanks in advance,

  • #2
    How do you get the data from the database? Are you using JDBC Inbound adapters?


    • #3
      Yes using int-jdbc:inbound-channel-adapter


      • #4
        You said you are running it as Unit test and Unite test are not meant to "assert-and-exit" so unless you do anything after creating Spring AC that becomes the last line of code in your test so unless you create some artificial block its the correct behavior. The real question is why are you doing it? I mean why are you creating AC in the test case without performing any assertions.


        • #5
          Sorry Oleg if I was not clear. Let me retry to explain it.
          In JUnit I am trying to assert the data in the file(generated by loading app-context) with the data in a expected file.

          Here is the code which is working (as i have used the Thread.sleep()).

          class DataExtractTest{
          public void test_01(){
          	new ClassPathXmlApplicationContext("app-context.xml");
          	// code for intialising actual n expected files
          When I try to run it without using Thread.sleep() then I get FileNotFoundException because it tries to look for the file which is not yet generated as query takes time to return the result.

          Hoping I am clear this time.



          • #6
            Yes its clear, but all it tells me that you are using polling resource which means once the AC is fully created the resources you are asserting might not be fully available since poling thread is different then your test thread.
            Thread.sleep would be OK in tests. Another option would be to have a shared CountDownLatch bootstrapped as a bean. When a resource is ready component that created it can issue latch.countDown(). Meanwhile in your test you do latch.await() which wioll block until resource is available etc...
            Hope that helps