【发布时间】:2016-04-01 23:07:05
【问题描述】:
我在代码中使用了“断言”。
@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 类方法,它会按预期工作。
@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 unit-testing junit mockito powermock