1
我無法模擬靜態調用。我不知道我做錯了什麼。無法使用JMockit模擬靜態調用
我剝了下來源代碼:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class OptionsBusinessEventFormatter {
void process() {
System.out.println("Testing testing ");
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("request = " + request);
System.out.println(request.getParameter("returnedURI"));
}
}
我的單元測試
@RunWith(JUnit4.class)
public class OptionsBusinessEventFormatterTest {
private OptionsBusinessEventFormatter optionsBusinessEventFormatter;
@Before
public void setUp() throws Exception {
this.optionsBusinessEventFormatter = new OptionsBusinessEventFormatter();
}
@Test
public void process_HappyPath() {
final MockHttpServletRequest request = new MockHttpServletRequest();
Map<String, String> map = new HashMap<>();
request.setParameters(map);
new Expectations() {
@Mocked ServletActionContext servletContext;
{
ServletActionContext.getRequest();
result = request;
}
};
this.optionsBusinessEventFormatter.process();
}
}
我碰到下面的錯誤。
java.lang.IllegalStateException: Native library for Attach API not available in this JRE
at mockit.internal.startup.AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(AgentLoader.java:88)
at mockit.internal.startup.AgentLoader.loadAgent(AgentLoader.java:47)
at mockit.internal.startup.AgentInitialization.loadAgentFromLocalJarFile(AgentInitialization.java:27)
at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:252)
at org.junit.runner.Runner.<clinit>(Runner.java:22)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:61)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at sun.tools.attach.WindowsVirtualMachine.<clinit>(WindowsVirtualMachine.java:185)
at mockit.internal.startup.AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(AgentLoader.java:66)
... 16 more
java.lang.NoClassDefFoundError: Could not initialize class org.junit.internal.runners.ErrorReportingRunner
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:61)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
下面
public class MockServletActionContext extends MockUp<ServletActionContext> {
private final MockHttpServletRequest request;
public MockServletActionContext(MockHttpServletRequest request) {
this.request = request;
}
@Mock void $init() {}
@Mock
public HttpServletRequest getRequest() {
return this.request;
}
}
給出我得到同樣的錯誤,我即使使用樣機實例嘗試。
如異常消息所示,執行測試的JRE缺少JMockit需要的Attach API本機庫。可能你正在使用一個普通的JRE,它確實不包含'attach.dll'。要解決此問題,請將IDE配置爲從完整的JDK安裝(即,' \ jre')使用JRE。 –
我已經安裝了Oracle Java 7 JDK並設置了JAVA_HOME並將$ JAVA_HOME/bin附加到現有的$ PATH變量中。 – Kartik
JMockit不使用像JAVA_HOME,PATH等環境變量。可能甚至連您的Java IDE(假設正在使用)都可以使用它們。如果通過IDE執行測試,則需要將其配置爲使用所需的JDK/JRE。 –