Writing tests with assertThrows

Hello.

I’m writing tests for my app and one of them has to check if my service throws an exception,

assertThrows(expected, given);

but platform wraps my given exception into RemoteException so it never matches the expected.

org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==> expected: <com.company.app.service.ParameterRecognitionException> but was: <com.haulmont.cuba.core.global.RemoteException>
  at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:65)
  at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:37)
  at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:2952)
  at com.company.app.core.MyServiceTest.checkNull(MyServiceTest.java:49)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
....
  at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
  at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: com.haulmont.cuba.core.global.RemoteException:
---
com.company.app.service.ParameterRecognitionException: BLOCK_NULL

should i unwrap it somehow like (pseudocode follows)

((RemoteException) () -> MyServiceTest.checkNull()).getCause()

or annotate the exception itself with something?

let me add a fancy screenshot of a problem and MWE))

mwe.zip (74.9 KB)

Take a look here: Passing Middleware Exceptions - CUBA Platform. Developer’s Manual

yep, annotated it… no luck it can be seen in my MWE. i can try to catch and unwrap it, but suppose there’s a more elegant way

sorry for upping this topic but i have a workaround (for those who is searching for answer) but still no solution.

my method throws ParamRecognitionException, annotated with @SupportedByClient and in tests it’s still RemoteException.

        final RemoteException exception = Assertions.assertThrows(
                RemoteException.class,
                () -> laTeXService.revealParameters(block));
        assertEquals("Null string check ", errMsg, exception.getMessage());

Hello Ivan!

To get a real exception class, you can use the following code:

final RemoteException exception = Assertions.assertThrows(
                RemoteException.class,
                () -> laTeXService.revealParameters(block));

Assertions.assertEquals(MyFancyException.class, exception.getFirstCauseException().getClass());
1 Like

so we unwrap it. thank you!