Java 为什么';断言';在junit测试中没有失败
我在代码中使用了“assert”Java 为什么';断言';在junit测试中没有失败,java,unit-testing,junit,mockito,powermock,Java,Unit Testing,Junit,Mockito,Powermock,我在代码中使用了“assert” @RunWith(PowerMockRunner.class) @PrepareForTest({ Puppy.class }) public class PuppyTest { @Test public void testCreatePuppy() { // Mocking Human human = Mockito.mock(Human.class); Puppy puppy = Puppy.
@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测试用例中使用断言吗
更新:
我通过将-ea作为vm参数传递来启用断言。它成功了。:)
要使java断言正常工作,您需要使用-ea标志运行应用程序/测试。尝试使用-ea标志。其中as
Assert
是特定于JUnit的,断言语句,与java断言无关。要使java断言正常工作,需要使用-ea标志运行应用程序/测试。尝试使用-ea标志。其中asAssert
是特定于JUnit的,断言语句,与java断言无关。顺便说一句,很多人(包括我)说,在测试中不应该使用普通断言(而不是断言类方法)。这使得在没有断言的情况下运行太容易了,这会使您对测试产生错误的信心。顺便说一句,很多人(包括我)说,您不应该在测试中使用普通断言(与断言类方法相反)。它使得在没有断言的情况下运行非常容易,这会使您在测试中产生错误的信心。