Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java:Double Brace Initialization #7

Open
CharLemAznable opened this issue Nov 1, 2019 · 0 comments
Open

Java:Double Brace Initialization #7

CharLemAznable opened this issue Nov 1, 2019 · 0 comments
Labels

Comments

@CharLemAznable
Copy link
Owner

在使用SonarQube检查使用Jmockit进行单元测试的Java代码时, 报出了一个Code Smell和一个Bug, 分别是:squid:S1171(Only static class initializers should be used)squid:S3599(Double Brace Initialization should not be used), 原因是使用了Jmockit的录制功能:

    @Test
    public void testInstanceMockingByExpectation() {
        AnOrdinaryClass instance = new AnOrdinaryClass();
        // 直接把实例传给Expectations的构造函数即可Mock这个实例
        new Expectations(instance) {
            {
                // 尽管这里也可以Mock静态方法,但不推荐在这里写。静态方法的Mock应该是针对类的
                // mock普通方法
                instance.ordinaryMethod();
                result = 20;
                // mock final方法
                instance.finalMethod();
                result = 30;
                // native, private方法无法用Expectations来Mock
            }
        };
        Assert.assertTrue(AnOrdinaryClass.staticMethod() == 1);
        Assert.assertTrue(instance.ordinaryMethod() == 20);
        Assert.assertTrue(instance.finalMethod() == 30);
        // 用Expectations无法mock native方法
        Assert.assertTrue(instance.navtiveMethod() == 4);
        // 用Expectations无法mock private方法
        Assert.assertTrue(instance.callPrivateMethod() == 5);
    }

搜索了一下Double Brace Initialization, 记录在此.
参考: Java:Double Brace Initialization

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant