Unit test: claim it doesn't work?

I only use UnitTest for a while, and today I met something very strange. Given the following code:

TestObject alo = null; assert alo != null; // Pass!!! Assert.assertNotNull(alo); // Fail, as expected. 

I searched googled and found that assert is Java built-in and assertNotNull is supported by JUnit. But I cannot understand why assert complains nothing about the null object?

+6
source share
3 answers

Hoang, I think you are a little confused between the statements of the Java language and the JUnit claims.

  • The assert keyword in Java was added in 1.4 and is intended to check for internal consistency in a class. Best practice recommendations were to use them in private methods to test program invariants. When an assertion fails, a java.lang.AssertionError is called and is generally not intended to be caught. The idea was that they could be turned on during debugging and turned off in production code (which is the default), but to be honest, I don’t think they were ever really caught. I have not seen them use a lot.

  • JUnit also claims as many different static methods in the org.junit.Assert package. They are intended to verify the results of this test. These statements also throw java.lang.AssertionError, but the JUnit framework is configured to catch and record these errors and generate a report of all failures at the end of the test run. This is a more common use of IMO statements.

Obviously, you can use one of them, but JUnit's statements are much more expressive, and you do not need to worry about enabling or disabling them. On the other hand, they are not intended for use in business code.

EDIT: Here is a sample code that works:

 import org.junit.Assert; import org.junit.Test; public class MyTest { @Test public void test() { Object alo = null; assert alo != null // Line 9 Assert.assertNotNull(alo); // Line 10 } } 

Here, the output from the launch with Java claims is disabled (by default):

 c:\workspace\test>java -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest JUnit version 4.8.1 .E Time: 0.064 There was 1 failure: 1) test(MyTest) java.lang.AssertionError: at org.junit.Assert.fail(Assert.java:91) ... at MyTest.test(MyTest.java:10) ... 

This runs with Java statements (-ea):

 c:\workspace\test>java -ea -cp bin;lib\junit-4.8.1.jar org.junit.runner.JUnitCore MyTest JUnit version 4.8.1 .E Time: 0.064 There was 1 failure: 1) test(MyTest) java.lang.AssertionError: at MyTest.test(MyTest.java:9) ... 

Note that in the first example, the Java assert passes, and in the second, the failure.

+10
source

The assert keyword is disabled by default for the Java virtual machine (see http://docs.oracle.com/cd/E19683-01/806-7930/6jgp65ikq/index.html ). Various Java tools may or may not be configured to include default statements.

Currently, Eclipse does NOT activate default statements when running JUnit tests. (See Discussion https://bugs.eclipse.org/bugs/show_bug.cgi?id=45408 - the reason why you should not maintain backward compatibility).

However, since Eclipse 3.6 has an easy way in Eclipse to ensure that statements are enabled by default for JUnit tests. Open the JUnit settings (Windows | Preferences | Java | Junit) and select the “Add - e” option in the VM arguments when creating a new JUnit startup configuration. "Note that this option will not change existing JUnit startup configurations, for which you will need to manually add the -ea parameter to the VM argument field for each such startup configuration.

+4
source

His mistake

His mistake is inside eclipse.

+1
source

Source: https://habr.com/ru/post/912561/


All Articles