1

I used 'assert' in my code.

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Puppy.class })
public class PuppyTest {

    @Test
    public void testCreatePuppy() {
        // Mocking
        Human human = Mockito.mock(Human.class);
        Puppy puppy = Puppy.createPuppy("Gatsby", human);
        assert(null!=puppy);
        assert("Gatsby1".equals(puppy.getName()));
        assert false;   //This should fail anyway
    }

  .....

} 

But , assert is not failing even for falsy conditions. If I use Assert class methods, it is working as expected.

  @Test
    public void testCreatePuppy() {
        // Mocking
        Human human = Mockito.mock(Human.class);
        Puppy puppy = Puppy.createPuppy("Gatsby", human);
        Assert.assertNotNull(puppy);
        Assert.assertEquals("Gatsby1",puppy.getName());
  }

Can't we use assert in Junit test cases ?

Update :

I enabled assertion via passing -ea as vm argument . And it worked. :)

enter image description here

Sujith PS
  • 4,776
  • 3
  • 34
  • 61

1 Answers1

4

For java assertions to work you need to run the application/test with -ea flag. Try with -ea flag. Where as Assert is a JUnit specific and asserts the statement and not related to java assertions.

  • 4
    btw, a lot of people (me included) say you should never use plain asserts (as opposed to Assert class methods) in tests. It makes it too easy to run without asserts, which gives you false confidence in your tests. – yshavit Dec 28 '15 at 05:12