jmockit 模拟 jacax.sql.DataSource

发布于 2024-10-02 07:45:52 字数 2765 浏览 4 评论 0原文

尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那支青花 2024-10-09 07:45:52

我不是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文