我正在尝试创建我的第一个测试。
我必须证明某个方法返回了ContextLambda类型,我正在使用assertSame函数对其进行测试,但是我的测试失败了,我不知道使用什么assert来测试它,而assertEquals也失败了。
我的测试是这样的:
@Test
public void testCanCreateContextForLambda() {
ContextFactory factory = new ContextFactory();
LambdaContext context = factory.forLambda(
new FakeRequest(),
new FakeResponse(),
new FakeLambda()
);
assertSame(LambdaContext.class, context);
}
最佳答案
尝试使用instanceof
和assertTrue
:
包括assertTrue导入:
import static org.junit.Assert.assertTrue;
然后进行实际测试:
@Test
public void testCanCreateContextForLambda() {
ContextFactory factory = new ContextFactory();
LambdaContext context = factory.forLambda(
new FakeRequest(),
new FakeResponse(),
new FakeLambda()
);
assertTrue(context instanceof LambdaContext);
}
只要
context
是类型LambdaContext
的类,此断言将是微不足道的,并且将始终为真(例如,使用接口使其变得平凡)。