簡體   English   中英

如何在被測 class 的注入實例上模擬實例方法?

[英]How to mock an instance method on injected instance of the class under test?

我正在嘗試模擬一個真實的 object 中的實例方法,該實例方法被注入到我在spock中的測試 class 中,並且我正在使用 Micronaut(使用 Java)編寫我的應用程序。 它是這樣的:

class MyTestClass extends Specification {
    @Subject
    @Inject
    ClassUnderTest classUnderTest

    Event sampleEvent = getMockEvent();

    def "test1" () {
       given:
       def classUnderTestSpy = Spy(classUnderTest)

       when:
       def res = classUnderTestSpy.callTestMethod(sampleEvent)

       then:
       1 * classUnderTestSpy.isRunning(_ as Long) >> {
           return false
       }
       res != null
    }

    def getMockEvent() {
       new Event(/* Some attributes here */)
    }
}

ClassUnderTest是這樣的:

@Singleton
class ClassUnderTest {
    @Inject
    Class1 class1Instance;

    @Value("${com.myapplication.property}")
    Integer value;


    Object callTestMethod(Event event) {
       // Some code
       boolean isRunning = isRunning(event.id);
       // Rest of the code
    }

    public boolean isRunning(Long id) {
        return SomeOtherClass.staticMethod(id);
    }
}

每當調用isRunning方法時,就會發生真正的SomeOtherClass.staticMethod()方法調用,並且isRunning方法返回 true 而不是 false。 無論如何我可以ClassUnderTest Spy並模擬實例方法嗎?

同樣,無法重現您的問題。

根據您的示例,我執行了以下操作並且測試通過了:

@MicronautTest //Your example is missing this, cannot even test without it
class SpyTest extends Specification{

    @Subject
    @Inject
    ClassUnderTest classUnderTest

    Event sampleEvent = getMockEvent();

    def "Spy Test"() {
        given:
        def classUnderTestSpy = Spy(classUnderTest)

        when:
        def results = classUnderTestSpy.callTestMethod(sampleEvent)

        then:
//        1 * classUnderTestSpy.isRunning(_ as Long) >> false //Preferred Groovy/Spock way
        1 * classUnderTestSpy.isRunning(_ as Long) >> {
            println("I am returning the correct thing!")
            false
        }
        results == "Is something running: false"
    }

    def getMockEvent() {
        new Event("Is something running: ")
    }
}
@Singleton
public class ClassUnderTest {
    private final InjectedClass iClass; //Noise in the example, not used

    public ClassUnderTest(InjectedClass iClass) { //Preferred way to inject
        this.iClass = iClass;
    }

    public String callTestMethod(Event event) {
        System.out.println("ClassUnderTest callTestMethod");
        boolean running = isRunning(0L);
        return event.message() + running;
    }

    public boolean isRunning(Long id) {
        System.out.println("ClassUnderTest isRunning with ID: " + id);
        return SomeOtherClass.staticMethod();
    }
}
@Singleton
public class InjectedClass {
    public String info() {
        return "I was injected";
    }
}

public record Event(String message) {}
@Singleton
public class SomeOtherClass {
    public static boolean staticMethod() {
        System.out.println("SomeOtherClass Static Method called");
        throw new RuntimeException("Should never get called");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM