V
V
Veniamin Belousov2018-06-29 12:57:27
Java
Veniamin Belousov, 2018-06-29 12:57:27

How to make Expected Exception in Unit Test a positive result?

A unit test has been written that passes certain parameters to the business service, under which the service issues a warning (error). This error is a positive result and is passed to the test result, making the TestNG result for . TestNG has this solution @Test(expectedExceptions = ErrorClass.class), but the service that is being tested has only one class, so if we apply this, we will not know about the error, which was actually not positive. While there is a solution - where we pass an additional parameter, that this is a test, and in this case it enters the function, but does not fall into Exeption, but I don’t like this solution. I must say right away: we cannot make it not an Exeption. Maybe somehow you can choose the right Exeption for some parameters ....
Your solutions are very interesting, sorry if I explained crookedly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bedward70, 2018-07-01
@Venuhaha

santaatnas correctly wrote in the comments: " what prevents you from executing this piece of code in a try block, and catching the necessary exception in the catch block and doing assertEquals by the name of the object
class
? " should be executed in the test.
Below is an example for testing a guaranteed exception:

import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;

public class ExampleTest {

    private void exceptionMethod() throws Exception {
        throw new RuntimeException("test");
    }

    @Test
    public void test() {
        try {
            exceptionMethod();
            fail("Impossible");
        } catch (Exception e) {
            assertEquals("Checks class", RuntimeException.class, e.getClass());
            assertEquals("Checks message", "test", e.getMessage());
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question