简体   繁体   English

为什么'assert'在junit测试中不会失败

[英]Why 'assert' is not failing in junit test

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. 但是,即使在虚假条件下,assert也不会失败。 If I use Assert class methods, it is working as expected. 如果我使用Assert类方法,它将按预期工作。

  @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 ? 我们不能在Junit测试用例中使用assert吗?

Update : 更新:

I enabled assertion via passing -ea as vm argument . 我通过传递-ea作为vm参数启用了断言。 And it worked. 而且有效。 :) :)

在此处输入图片说明

For java assertions to work you need to run the application/test with -ea flag. 为了使Java断言起作用,您需要使用-ea标志运行应用程序/测试。 Try with -ea flag. 尝试使用-ea标志。 Where as Assert is a JUnit specific and asserts the statement and not related to java assertions. 其中, Assert是特定于JUnit的,并且断言该语句,并且与Java断言无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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