-1

guys:

I have a question about Junit test as a newbie: I tried to build a JAR project. it failed and reported that a function in Junit test failed, but when I run that Junit test, it was successful. my teammate suggested me to check surefire-reports, and I did find that this Junit test has a failure report. I wonder why I could run Junit test correctly from eclipse while it actually failed?

lxiscas
  • 141
  • 1
  • 2
  • 13

3 Answers3

0

Usually it is one of a few things:

  • Difference in environment: Windows, Linux, directory structure
  • Dependencies: Eclipse always uses loaded projects for dependencies, but when compiling in Maven it will use dependencies from the repository. Check that all dependencies are updated in repo.
  • Threading. Eclipse runs one test at a time. Maven will run multiple concurrently.

Without seeing the test and having more details of you env it is hard to give anything more specific.

John B
  • 32,493
  • 6
  • 77
  • 98
0

Without seeing the code, it is impossible to give the solution. However, there may be many reasons why the test fails sometimes and not others:

  1. Different environment - different versions of java or other environment issues (free memory, access to files on file systems) may make the test work in one environment and not on another.
  2. Fragile Tests - The test may work sometimes and not others due to a poor test that only works sometimes
    • using random input data that isn't always valid
    • the time of day - I've seen tests that only pass during business hours and fail on nightly builds.
    • timeouts - tests fail if they take too long that are dependent on scheduler or other systems that have erratic response times.
    • threading issues/ deadlock
dkatzel
  • 31,188
  • 3
  • 63
  • 67
0

Guys: Thank you for your help. I checked code per your suggestion. It looks the code doe snot have anything to do with timing issue or threading. and to my surprise, the data that the test checked was missing from remote Oracle server, how can eclipse run this Junit function correctly is still mysterious to me. When I tracked it in debugging mode, it showed that one of assertion should fail actually. I did not change environment, it is local tomcat/windows. I suspect it is due to memory leaking, or as someone said, I should run whole Junit test code of this project to check since there might be some code sharing data. Anyway, thank you for your suggestion. I am sorry for that I am not allowed to publish sensitive code online.

Xin Li (Sam)

lxiscas
  • 141
  • 1
  • 2
  • 13