2011-10-26 100 views
0

以下TestNG的“为预期的记录无效的上下文中,”失败(6.3)测试情况下产生错误“无效上下文的期望的记录”TestNG的测试用例与JMockit

@Listeners({ Initializer.class }) 
public final class ClassUnderTestTest { 

private ClassUnderTest cut; 

@SuppressWarnings("unused") 
@BeforeMethod 
private void initialise() { 
    cut = new ClassUnderTest(); 
} 

@Test 
public void doSomething() { 
    new Expectations() { 
     MockedClass tmc; 
     { 
      tmc.doMethod("Hello"); result = "Hello"; 
     } 
    }; 
    String result = cut.doSomething(); 
    assertEquals(result, "Hello"); 
} 

}

被测试的班级如下。

public class ClassUnderTest { 

MockedClass service = new MockedClass(); 
MockedInterface ifce = new MockedInterfaceImpl(); 

public String doSomething() { 
    return (String) service.doMethod("Hello"); 
} 

public String doSomethingElse() { 
    return (String) ifce.testMethod("Hello again"); 
} 
} 

我在做,因为我现在用的是@Listeners注解,我不需要javaagent命令行参数的假设。这个假设可能是错误的......

任何人都可以指出我错过了什么?

回答

1

JMockit-TestNG Initializer必须在整个测试运行中运行一次,因此在单个测试类中使用@Listeners将不起作用。

相反,只需升级到JMockit 0.999.11,该工具可以透明地与TestNG 6.2+配合使用,无需指定侦听器或-javaagent参数(除非在JDK 1.5上运行)。

+0

谢谢,只是版本更新排序的问题。带你点关于听众。谢谢(你的)信息。 – sweetfa