0

Just like the link, I have met a same question. I write a code, which runs well on my local machine but could not work on a linux machine. The code is as follows:

public class BaseCase{
public static ServiceFactory factory = ServiceFactory.getInstance();
static{
//the factory is a container, and code here is to insert some processes into it,and there is a try catch
}
}

and the error is as follows:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec <<< FAILURE! - in xxx.足迹找相似.P1_足迹着相似_回归_Tes
xxx.足迹找相似.P1_足迹着相似_回归_Test  Time elapsed: 0 sec  <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class com.taobao.testcase.BaseCase
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

This error happens when I used

mvn -U clean  test -Dtest "path"

on the linux machine.

So, where is the problem? That is really strange.

PS:though I think I have met the same question in the link above,but I think there is something different. Firstly, the answer said that the problem is in the static block, but I used a try catch,but catch nothing; secondly, I think would it because I start the container failed? It has already confused me for days.

Community
  • 1
  • 1
MonkeyKing
  • 53
  • 10
  • @John Vint , I watched your answer in this [link](http://stackoverflow.com/questions/7325579/java-lang-noclassdeffounderror-could-not-initialize-class-xxx), but I still can't solve this question. So are those questions the same? – MonkeyKing Oct 26 '15 at 02:07
  • John Vint won't be notified of your comment because he hasn't commented on your question. Have you tried running your tests in a debugger and stepping through the static initialiser to see if it at least crashes on a specific line? Hard to tell what could be wrong if you elided all the code that could fail. – millimoose Oct 26 '15 at 02:26
  • As I said above, It works well on my local machine and the Linux machine doesn't allow me to debug! What's more, the class "足迹找相似.P1_足迹着相似_回归_Test" extends BaseCase. Thanks for you reminding after all, I'll try to show some key code, and connect to John Vint. – MonkeyKing Oct 26 '15 at 02:49
  • How is your try-catch block set up? If it's `catch (Exception e)`, it won't catch all `Throwable`s, notably not `Error`s. (Although it is odd that the Maven test runner would swallow a nested `Error` like that.) Also, if you can't debug on the Linux machine, try running your on a "clean" system you control (like a virtual machine you set up) the same way as it's executed on the Linux machine, and debug it through a network? – millimoose Oct 26 '15 at 03:02
  • According to your suggestion, I tried several times. And I found it very strange.At first, I'll introduce to your the way I test.There're two tasks as follows : (1)mvn -U clean test -Dtest "path1"; (2)mvn -U clean test -Dtest "path2". BTW, "path1" and "path2" are in the same project. When I run task one or task two separately , they run fine just like on my local machine. But they turn out error when run together at the same time. And the error occurred randomly. That's to say, It turns out right sometime and wrong the rest. – MonkeyKing Oct 26 '15 at 03:55
  • I'm so sorry that I can only show your part of the code as this code is confidential in my company. – MonkeyKing Oct 26 '15 at 04:02
  • Unfortunately that also probably means that's where anyone that can solve this is, seeing as empty blocks very rarely fail. That's where I was getting at with the debugging suggestion - it could help you isolate the problem to only those lines of code that break, and hopefully you would end with few enough to post a test case. – millimoose Oct 26 '15 at 04:09
  • Also, an out-of-left field suggestion: refactor your code so it doesn't use nontrivial static initialisers. (Dependency injection should let you easily extract "run-once" code into singletons.) That way errors in that code will manifest as part of normal program flow, not buried in an `Error,` and hopefully you'll get a more useful stack trace. – millimoose Oct 26 '15 at 04:12
  • You're right. But this API is only can be used with static. Thank you anyway. It seems that I should find the developer for help. – MonkeyKing Oct 26 '15 at 06:16

0 Answers0