繁体   English   中英

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

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

我在代码中使用了“断言”。

@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
    }

  .....

} 

但是,即使在虚假条件下,assert也不会失败。 如果我使用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());
  }

我们不能在Junit测试用例中使用assert吗?

更新:

我通过传递-ea作为vm参数启用了断言。 而且有效。 :)

在此处输入图片说明

为了使Java断言起作用,您需要使用-ea标志运行应用程序/测试。 尝试使用-ea标志。 其中, Assert是特定于JUnit的,并且断言该语句,并且与Java断言无关。

暂无
暂无

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

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