Search code examples
exceptionjunitassertjunit5

Junit 5 : AssertionFailedError: expected: <true> but was: <false>


I'm trying to write a unit test case for the below function in the service class

@Service
    public class currencyHandler {
        public boolean isNoDecimal(String currency) {
            return "JPY".equalsIgnoreCase(currency) || "KRW".equalsIgnoreCase(currency);
        }
    }

Below is the unit case:

    import org.junit.jupiter.api.Test;
    import static org.junit.jupiter.api.Assertions.assertFalse;
    import static org.junit.jupiter.api.Assertions.assertTrue;
    import org.springframework.test.context.junit.jupiter.SpringExtension;
    import org.svc.it.handlers;
    
    @ExtendWith(SpringExtension.class)
    public class CurrencyTests {
    
        @Mock
        CurrencyHandler currencyHandlerTest;
    
        @Test
        public void isNoCurrency() throws Exception {
            assertTrue(currencyHandlerTest.isNoCurrency("ABC"));
            assertTrue(currencyHandlerTest.isNoCurrency("XYZ"));
            assertFalse(currencyHandlerTest.isNoCurrency("BOGUS"));
            assertFalse(currencyHandlerTest.isNoCurrency(""));
            assertFalse(currencyHandlerTest.isNoCurrency(null));
        }
    }

Below is the error when I run above unit test case:

org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
    at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
    at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:40)
    at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:35)
    at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:162)

I guess I'm doing something wrong with @Mock object. Appreciate your help. Thanks in advance!


Solution

  • To write a unit test for the CurrencyHandler class, you must test against an instance of that class. You must not test against an instance of a mock of that class.

    The following shows how to write such a unit test.

    package example;
    
    import org.junit.jupiter.api.Test;
    
    import static org.junit.jupiter.api.Assertions.assertFalse;
    import static org.junit.jupiter.api.Assertions.assertTrue;
    
    class CurrencyHandlerTests {
    
        private CurrencyHandler currencyHandler = new CurrencyHandler();
    
        @Test
        void isNoDecimal() {
            assertTrue(currencyHandler.isNoDecimal("JPY"));
            assertTrue(currencyHandler.isNoDecimal("KRW"));
            assertFalse(currencyHandler.isNoDecimal("BOGUS"));
            assertFalse(currencyHandler.isNoDecimal(""));
            assertFalse(currencyHandler.isNoDecimal(null));
        }
    }
    

    To better understand the concepts here, I recommend that you read some articles or books on unit testing, mocks, and the SUT (subject under test).

    In addition, you do not need to register the SpringExtension if you are writing a pure unit test like the one above, since a pure unit test does not need to interact with components in a Spring ApplicationContext.