Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:00:28+00:00 2026-06-01T14:00:28+00:00

I’m applying UnitTest just for a while, and today I met something very strange.

  • 0

I’m applying UnitTest just for a while, and today I met something very strange. Considering the following code:

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

I googled around and find that assert is java-built-in, while assertNotNull is JUnit supported. But I can’t understand why assert don’t complain anything about the null object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:00:29+00:00Added an answer on June 1, 2026 at 2:00 pm

    Hoang, I think you’re getting a little confused between Java language asserts and JUnit asserts.

    • The assert keyword in Java was added in 1.4, and intended to allow to verify internal consistency in a class. The best practice recommendation was to use them in private methods to test program invariants. When an assert fails, a java.lang.AssertionError is thrown and is generally not intended to be caught. The idea was they could be enabled during debugging and disabled in production code (which is the default), but frankly, I don’t think they ever really caught on. I haven’t seen them used much.

    • JUnit also has asserts, in the form of many different static methods in the org.junit.Assert package. These are intended to verify the results of a given test. These asserts also throw a java.lang.AssertionError, but the JUnit framework is set up to catch and record those errors and to generate a report of all failures at the end of the test run. This is the more common usage of asserts, IMO.

    You can obviously use either one, but the JUnit asserts are far more expressive, and you don’t have to worry about enabling or disabling them. On the other hand, they aren’t really for use in business code.

    EDIT: Here’s a code example 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’s the output from a run with Java assertions disabled (the 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)
      ...
    

    Here’s a run with Java assertions enabled (-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)
      ...
    

    Notice in the first example, the Java assert passes, and in the second, it fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.