jmockit 模拟 jacax.sql.DataSource
尝试使用 jmockit 来删除 javax.sql.DataSource 类。有什么想法吗?
public class MyTest
{
@MockClass(realClass = DataSource.class)
public static class MockDataSource {
}
@Before
public void setup()
{
Mockit.setUpMocks(MockDataSource.class);
}
@Test
public void testCreateGetUpdateUser()
{
new NonStrictExpectations()
{
...
};
...
}
}
我在 eclipse 中运行测试并得到这个
java.lang.IllegalArgumentException: Not a modifiable class: javax.sql.DataSource
at mockit.internal.RedefinitionEngine.createClassReaderForRealClass(RedefinitionEngine.java:222)
at mockit.internal.RedefinitionEngine.modifyRealClass(RedefinitionEngine.java:207)
at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:199)
at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:193)
at mockit.Mockit.setUpMocks(Mockit.java:274)
at MyTest.setup(EntityManagerUTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:122)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)
Trying to use jmockit to stub out a javax.sql.DataSource class. Any ideas?
public class MyTest
{
@MockClass(realClass = DataSource.class)
public static class MockDataSource {
}
@Before
public void setup()
{
Mockit.setUpMocks(MockDataSource.class);
}
@Test
public void testCreateGetUpdateUser()
{
new NonStrictExpectations()
{
...
};
...
}
}
I run the test in eclipse and get this
java.lang.IllegalArgumentException: Not a modifiable class: javax.sql.DataSource
at mockit.internal.RedefinitionEngine.createClassReaderForRealClass(RedefinitionEngine.java:222)
at mockit.internal.RedefinitionEngine.modifyRealClass(RedefinitionEngine.java:207)
at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:199)
at mockit.internal.RedefinitionEngine.redefineMethods(RedefinitionEngine.java:193)
at mockit.Mockit.setUpMocks(Mockit.java:274)
at MyTest.setup(EntityManagerUTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:122)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 jmockit 的用户(我更喜欢 http://mockito.org/ ,它看起来更容易使用),但是我认为你的 @MockClass 不完整 - 也许你需要一个 @Mock 方法:
http://jmockit.googlecode.com/svn/trunk/www/tutorial/StateBasedTesting.html#interfaces
I am not a user of jmockit (I prefer http://mockito.org/ which seems simpler to use), but I think that your @MockClass is not complete - mabybe you need a @Mock method in it:
http://jmockit.googlecode.com/svn/trunk/www/tutorial/StateBasedTesting.html#interfaces