是否可以断言像Java Mockito一样通过Go中的间谍程序调用了真正的方法?

I am looking for asserting that a statement is covered in my test. For instance, let's say from test am calling methodA() which has reference to methodB().

I would like to assert that methodB() is called upon executing methodA() from tests.

In the following code how do I assert in A Go test that svc.AddCheck() is called on execution of svc.OnStartup()?

func (svc *Servjice) OnStartup() error {
    if err := svc.AddCheck("cache"); err != nil {
        return err
    }
    return nil
}

Is it possible to assert that a real method is called via spying in golang like Java Mockito?

No, not at all.

Is it possible to assert that a real method is called via spying in Go like Java Mockito?

In a word: No.

But what about alternatives?

What you should be testing for is the effects of your function. This is true in any language, not just Go. This is also true whether you're doing unit testing or some higher-level type of testing, including end-to-end testing.

If your methodB() calls methodA(), it's not useful to check, per se, that methodA() is called. What is useful is to check that methodB() does what you expect. Whether it uses methodA() to accomplish that or not, is an implementation detail that your test, by definition, should not care about.

Some examples:

  • Suppose you're writing an integration test, and methodA() inserts something into the database. Don't assert that methodA() is called--instead assert that the row you expect is now in the database.

  • Suppose you're writing a unit test for a Fibonacci generator. Don't assert whether it calls methodA(), assert whether it returns the correct value.

When you assert that a sub-method is called, you're worried about implementation details--the "how" of your function. That's not what good tests do. Good tests assure that your method produces the correct result. It shouldn't matter, from a testing standpoint, whether it does that by calling methodA(), methodZ(), a REST API, or using Jedi powers.

So TL;DR; No, you can't do what you're asking, but that's okay because you shouldn't do what you're asking. There are better alternatives (even in Java).