JUnit #BeforeClass annotation compiles fine in IDE but fails Maven build - java

When I write a simple method as follows:
#BeforeClass
public void setUp(){}
it compiles properly both in IntellijIDEA and Eclipse (using built in IDE Build).
However the same fragment of code fails a Maven run:
Tests in error:
initializationError(TestClass): Method setUp() should be static
Should have not this error been caught during compilation?

The retention type of #BeforeClass annotation is RUNTIME, thus it will be reported when you execute it.

This is no different than a test failure, which is only ever detected at runtime. Because you have a malformed method attached to your #BeforeClass annotation, JUnit is informing you that the tests cannot run due to that, thus failing the build.
Compilation has nothing to do with this. You're going to want to, well, fix your tests to ensure that they comply with what JUnit is asking for.

Related

Eclipse listing but not running unit tests, how to fix this?

When running a specific junit test in the Apache Jackrabbit project, Eclipse goes through the motions of running the test, but the test is never run, debugger breakpoints are never fired, and the results are that zero out of eight tests were run, and no errors were reported.
https://github.com/apache/jackrabbit/blob/trunk/jackrabbit-spi2dav/src/test/java/org/apache/jackrabbit/spi2dav/ConnectionTest.java
The confusion is that the tests, after the run, don't indicate whether they succeeded, failed, or were explicitly skipped, and I cannot see anything in the source to indicate they should be skipped.
What is Eclipse trying to tell me?
This behaviour was triggered by the unit test overriding Junit's run method as follows:
https://github.com/apache/jackrabbit/blob/ed3124e5fe223dada33ce6ddf53bc666063c3f2f/jackrabbit-jcr-server/src/test/java/org/apache/jackrabbit/webdav/server/WebDAVTestBase.java#L234
public void run(TestResult testResult) {
if (Boolean.getBoolean("jackrabbit.test.integration")) {
super.run(testResult);
}
}
This was causing the test to not run at all.
In this case, Eclipse was picking up the test methods that should have been run and listed them, but with the tests not having been run, the Eclipse UI remains in a "vanilla" state.
This unit test obviously needs to be fixed to follow maven's best practise of splitting unit and integration tests into surefire and failsafe respectively, and not relying on a private mechanism.

Migration to JDK11 changes how tests must be run from Maven

I'm upgrading my code from Java v1.8 to v11.0.8. All went fine until I run the tests mvn test and Mockito starts having issues. Every test errors-out.
My versions of Mockito are fine (2.23.4) according to other answers here so I go to run just 1 test to narrow down the problem - it runs fine. So I run all the tests by right-clicking in Eclipse on my src/test/java and choosing Run As > JUnit test. Again, all run fine.
Note: I hesitate to add the error message here because the code works (as noted above), so this can be a distraction, but this question got a close request for lack of error message!
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn more about inline mocks and their limitations under #39 of the Mockito class javadoc
Underlying exception: org.mockito.exceptions.base.MockitoException: Could not modify all classes [..., ..., ...]
at ...
at ...
Caused by org.springframework.beans.BeanInstantiationException: Failed to instantiate [..]: Factory method '...' threw exception; nested exception is org.springframework.beans.BeanCreationException: Error creating bean with name '...' defined in...
Mockito cannot mock this class: ...
If you're not sure why you're getting this error, please report to the mailing list.
The app itself runs fine with springboot:run clean
My tests always ran with only test as the goal, that is confirmed for me here, so what's changed?
I also see references to: Bytebuddy in that article but I have a good version of that too (1.9.16)

Junit works individually in IntelliJ but fails from Maven

I have some junit test cases within a test class. It runs fine when you run the file as a whole from IntelliJ.
But when I run the maven build, it fails.
TestCase Code Snippet {
doReturn(DummyObject with values).when(springRepo).call()
executeTest()
}
We have a TestUtil which has some static methods to create DummyObject at runtime based on different input keys. What I see that Junit always runs testcases sequentialy and there are no parallel test cases run unless specified.
Now, Dummy object is not null, but few of the internal data is null only from Maven run. What could be the issue?

JUnit test, Unrooted Tests: initializationError

So I edited the name of a JUnit test and now it wont work. Instead I get Unrooted Tests: initializationError.
This is a simple test. Infact it is a test for JUnit tests as I am just starting to use it.
#Test
public void testRun()
String s = null;
assertNull(s);
}
and all i did was change it to testRun2(). Also when I run the file not the individual test, it still runs the old testRun(), not testRun2().
My project has Maven not sure if that is a factor. And I have updated the project
So it turned out that I needed to rebuild using Maven to update the classes. Now it works fine and I can add/modify test cases.
In my case, i changed the method name and it didn't update it automatically, so the above solution of Project> Clean worked well for me.
Another way this error would occur is forgetting the Test annotation. Encountered when right click method name in Eclipse and Run As -> Junit Test.

not all junit tests are running in eclipse

I have a java project in eclipse, when I press the project right click -> run as junit some tests do not run. I attached a picture, see YamiMailSenderTest for example.
When I try to run the tests directly they are running.
I am using eclipse 3.7.2.
and expanded view:
Any idea?
Ran into the same problem, my error was that I wrote: public void myMethodName(){ //assertions }
instead of: public void testMyMethodName() { //assertions }
the test before the MyMethodName is important.
It's a bit late, but in case anyone finds this via a search engine:
If a Test is run multiple times the results provided by JUnit are indistinguishable for those Tests and thus the results are only displayed for one run. See also the following Eclipse bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=172256
Check if you are excluding tests from run by attributes and check under Run > Run Configurations if your JUnit configuration are excluding any tests.
In jUnit 4, a test case needs to have #Test annotation. The test case can be set to ignore with #Ignore annotation. The whole test class can also be set to ignore by placing the #Ignore annotation right above the class declaration. Note: In jUnit 4 , there is no need to extend Testcase class as in jUnit 3. Everything is in annotation.
I have no idea about jUnit 3 since I use only 4.
I had a similar problem. For some reason, the "Run As -> jUnit Test" was always skiping the first test package. I was on an older version of Eclipse and SpringSource.
I moved back to Juno - Version: 4.2.1 and all my test run when I perform: "Run As -> jUnit Test. "
I had the same problem. Eclipse would only recognize and run 5 out of my 9 tests. After much troubleshooting I found this trick to convince Eclipse to recognize the remaining tests: just open each file, hit space and then backspace to mark it as changed, and save it. Then, Eclipse will recognize it as a test.

Categories