How do I write a unit-test to throw an exception from the Mock method of Operation return type?

Righteouslee

I would like to write a unit-test to throw an exception from the Mock method of Operation return type.

I'm writing the unit-test with Spock in Groovy.

There are class A, and B

// class A

private ClassB b;

Promise<String> foo() {
    return b.methodX()
        .nextOp(s -> {
            return b.methodY();
        });
}

Return type of methodP() is Promise<> Return type of methodO() is Operation

// class B
public Promise<String> methodP() {
    return Promise.value("abc");
}

public Operation methodO() {
    return Operation.noop();
}

Unit-test for foo() method of Class A Mocking ClassB in the unit-test

// Spock unit-test

ClassA a = new ClassA()
ClassB b = Mock()

def 'unit test'() {
    given:

    when:
    execHarness.yield {
        a.foo()
    }.valueOrThrow

    then:
    1 * b.methodP() >> Promise.value("some-string")
    1 * b.methodO() >> new Exception("my-exception")

    Exception e = thrown(Exception)
    e.getMessage() == "my-exception"
}

I expected the Exception is thrown, but GroovyCaseException was thrown and test failed.

Error message says,

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'java.lang.Exception: my-exception' with class 'java.lang.Exception' to class 'ratpack.exec.Operation'
cgrim

Change this line:

1 * b.methodO() >> new Exception("my-exception")

on:

1 * b.methodO() >> { throw new Exception("my-exception") }

Because methodO() is not expected to return Exception instance (as in your example) but it is expected to be thrown (by using closure).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do i write unit test for function with void return type

How can I mock a void method to throw an exception?

How do I unit test this doFilter() method?

How to android unit test and mock a static method

How to write unit test using mock object?

How to test that the method doesn't throw an exception?

Junit how to mock method return in test method?

How do I mock a class in a Python unit test?

When unit testing, how do I mock a return null from async method?

How do i mock UserManager and RoleManager for unit test

How do I write an async unit test method in F#

Using Guice, how do I inject a mock object from my unit test, into the class being tested

How to mock the static method with mockito to do unit test

Jest unit test with Luxon: how do I mock .setZone('local')

How do I write a unit test for a Flutter method that completes later with a future?

How can I mock the result of a method in my unit test?

How do I mock the result of a promise in an AngularJS unit test?

How do I mock a method with void return type in JMockit?

How can I throw exception inside a Generic Type Method?

How to write mock unit test case for ExecuteNonQuery & ExecuteScalar & GetDataSet method

how can I mock a service to throw an exception a method that returns a List?

mock method to do not throw an exception

How to mock a method within an async unit test?

Cakephp unit test how I will write a test method for test buildRules

How do I create an unit test expectation for a method that I imported from external package?

How do I determine from the documentation what type of exception a function can throw?

How do I write a unit test that successfully recognizes that a function threw an exception?

How to unit test method with IAsyncEnumerable<dynamic> return type

How to mock method that has return type from of the (bool, string)?