简体   繁体   中英

Eclipse, and JUnit. Tests passing/failing depending on where they are run from?

Hello I have a problem that is driving me crazy. I have created a unit test that tries to instantiate an object. If it cannot, it throws an exception

 try{
  testObject = new Target(2,4,true);
 }
 catch(Exception e){
      AssertionError ae = new AssertionError("Unable to create a target");
      ae.initCause(e);
      throw ae;
}

I have then placed this into a JUnit test suite. If I select the suite, or the test by itself and click run, I get no problems.

On the other hand, in Eclipse IDE, if I right click the entire project folder and click run JUnit tests, the tests that were passing are now giving me errors such as indexoutofbounds. This test doesn't access any external files? so how can the running location make a difference. Does Eclipse do something weird here?

Also, is there any way to not have the individual tests run once, and then all run again when it reaches the testing suite file.

testCreateTarget(unit.targetTest) java.lang.AssertionError: Unable to create a target at unit.targetTest.testCreateTarget(TargetTest.java:26) 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 junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

The problem was the tests executed beforehand altered the state of the program. It seems there is some sort of continuous execution of tests, with modified variables being kept throughout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM