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

Both are executed when Mock Static void method #355

Closed
hellsof opened this issue Apr 18, 2023 · 6 comments · Fixed by #357
Closed

Both are executed when Mock Static void method #355

hellsof opened this issue Apr 18, 2023 · 6 comments · Fixed by #357

Comments

@hellsof
Copy link

hellsof commented Apr 18, 2023

The following example will pass the test:

final class Static {
    public static void tVoid() {
        throw new UnsupportedOperationException();
    }
}

class StaticTest extends Specification {
    def 'tVoid'() {
        given:
        def runMock = []
        Spy(Static)
        Static.tVoid() >> {runMock << true}

        when:
        Static.tVoid()

        then:
        runMock[0] == true
        thrown(UnsupportedOperationException)
    }
}
@joke
Copy link
Owner

joke commented Apr 20, 2023

@hellsof the interaction needs to be added to the then block. Otherwise you are still executing the method and shifting it. Same goes for non static methods.

@hellsof
Copy link
Author

hellsof commented Apr 20, 2023

There is no such problem after trying the non-static method, can you give a correct sample code, thanks.

@joke
Copy link
Owner

joke commented Apr 22, 2023

@hellsof I didn't get the problem you were talking about. But now I understand. You are absolutely right there is a bug in the interception code.

@hellsof
Copy link
Author

hellsof commented Apr 22, 2023

Yes, an exception should not be thrown here, but it is thrown when it is actually executed, causing the above test case to pass the test.

@hellsof
Copy link
Author

hellsof commented Apr 22, 2023

Perhaps the following example can better demonstrate this problem:

final class Static {
    public static void tVoid(List<String> list) {
        list.add("old");
    }
}

class StaticTest extends Specification {
    def 'tVoid'() {
        given:
        Spy(Static)
        List<String> param = new ArrayList<>()

        when:
        Static.tVoid(param)


        then:
        1 * Static.tVoid(_) >> {ArrayList<String> list ->
            list << "mock"
        }

        expect:
        param == ["mock"] // Condition not satisfied: param=["mock", "old"]
    }

joke added a commit that referenced this issue Apr 23, 2023
Signed-off-by: Joke de Buhr <joke@xckk.de>
joke added a commit that referenced this issue Apr 23, 2023
Signed-off-by: Joke de Buhr <joke@xckk.de>
joke added a commit that referenced this issue Apr 23, 2023
Signed-off-by: Joke de Buhr <joke@xckk.de>
@joke joke closed this as completed in c76db09 Apr 23, 2023
@joke
Copy link
Owner

joke commented Apr 23, 2023

@hellsof The problem should be fixed now.
Thanks for reporting the problem

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

Successfully merging a pull request may close this issue.

2 participants