标签:des style http os java 使用 io strong for
一、项目环境:
WIN7 64位,Eclipse 4.2,JDK1.6,JUnit 4.11,JMockit 1.10
二、示例代码:
import static org.junit.Assert.*; import org.junit.runner.RunWith; import cn.open.dao.AppDao; import cn.open.ddl.App; import cn.open.service.AppService; import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit4.JMockit; public class Test { @Mocked AppDao unused = null; @org.junit.Test public void test() { final App app = new App(); app.id = 22; app.adStatus = 1; app.appName = "hello"; new Expectations() { { AppDao.findById(anyInt); result = app; } }; App re = AppService.get(1); assertEquals(app, re); } }
使用JUnit + JMockit进行单元测试,运行JUnit Case时,抛出以下错误:
java.lang.IllegalStateException: JMockit wasn‘t properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)
at cn.uc.appstore.open.Test$1.<init>(Test.java:26)
at cn.uc.appstore.open.Test.test(Test.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
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)
三、解决方法:
1. 添加以下JUnit运行环境参数:
-javaagent:path/to/your/jmockit/jmockit-1.10.jar
2. 配置Maven插件绕过该问题:
JMockit一定要在JUnit之前:
<!-- JMockit must be before JUnit in the classpath --> <dependency> <groupId>mockit</groupId> <artifactId>jmockit</artifactId> </dependency> <!-- Standard unit testing -->< dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
确保插件已配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.3</version> <configuration> <argLine>-javaagent:${settings.localRepository}/mockit/jmockit/0.998/jmockit-0.998.jar</argLine> <useSystemClassLoader>true</useSystemClassLoader> </configuration> </plugin>
3. 使用@RunWith注解
@RunWith(JMockit.class) public class Test {}
四、结论
解决方法1,3经过测试能够解决上述错误,解决方法2未测试。
五、参考
标签:des style http os java 使用 io strong for
原文地址:http://my.oschina.net/dexterman/blog/307039