简体   繁体   中英

Junit Test Keeps Failing with IllegalArgumentException

I've tried placing try and catch blocks as well as with throw new IllegalArgumentException but none of this stops the failed test. What am I missing here? Thank you.

package com.udacity.jdnd.course1.service;

public class FizzBuzzService {

    /**
     * If number is divisible by 3, return "Fizz". If divisible by 5,
     * return "Buzz", and if divisible by both, return "FizzBuzz". Otherwise,
     * return the number itself.
     *
     * @Throws IllegalArgumentException for values < 1
     */
    public String fizzBuzz(int number) {

        String result = "";
        try {
            if (number % 3 == 0 && number % 5 == 0) {
                result = "FizzBuzz";
            } else {
                if (number % 3 == 0) {
                    result = "Fizz";
                } else if (number % 5 == 0) {
                    result = "Buzz";
                } else {
                    return Integer.toString(number);
                }
            }

            if (number == 0){
                throw new IllegalArgumentException("Value is zero ");
            }

//            return result;
//        }
    } catch(IllegalArgumentException i){
      System.out.println("this what is thrown " + i);
    }

        return result;

    }


}
package com.udacity.jdnd.course1;

import com.udacity.jdnd.course1.service.FizzBuzzService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class Course1ApplicationTests {

    @Test
    void testFizzBuzz(){
        FizzBuzzService fbs = new FizzBuzzService();

        // check non-divisible numbers return themselves
        assertEquals("1", fbs.fizzBuzz(1));
        assertEquals("47", fbs.fizzBuzz(47));
        assertEquals("121", fbs.fizzBuzz(121));

        // check numbers divisible by 3
        assertEquals("Fizz", fbs.fizzBuzz(3));
        assertEquals("Fizz", fbs.fizzBuzz(333));

        //check numbers divisible by 5
        assertEquals("Buzz", fbs.fizzBuzz(5));
        assertEquals("Buzz", fbs.fizzBuzz(85));

        //check numbers divisible by 3 and 5
        assertEquals("FizzBuzz", fbs.fizzBuzz(15));
        assertEquals("FizzBuzz", fbs.fizzBuzz(75));

        //check invalid inputs
        assertThrows(IllegalArgumentException.class, () -> fbs.fizzBuzz(0));
        assertThrows(IllegalArgumentException.class, () -> fbs.fizzBuzz(-1));
    }
}

The error I get is:

org.opentest4j.AssertionFailedError: Expected java.lang.IllegalArgumentException to be thrown, but nothing was thrown.

at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:71)

By placing this at the beginning and getting rid of the try/catch block

if (number == 0 || number == -1){
                throw new IllegalArgumentException("Value is zero ");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM