-1

I want help in finding the number of defects in the below java piece by using unit testing.

I only found 3, but I want to make sure that I found all of them. I would really appreciate it if somebody could help me with this. Thank you. Bellow are the comments about how this piece of code should work.

// constructor
// Throws IllegalArgumentException if invalid value
// Exception message for invalid value: "One or more of the parameters have invalid value"
// t parameter can only be upper case characters: E, D, A  
// n parameter must be a value between 10000 and 99999 inclusively
// p parameter must be greater than 0
// when all values are valid, assigns t to attribute type, n to attribute number and p to attribute price

Here is the script

public Cars(char t, int n, int p)
{
    if (t != 'E' && t != 'D' && t != 'A')
        throw new NullPointerException("One or more of the parameters have invalid value");

    if (n < 10000 || n >= 99999)
        throw new IllegalArgumentException("One parameter has invalid value");

    if (p <= 0)
        throw new IllegalArgumentException("One or more of the parameters have invalid value");

    type = t;
    number = n;
    price = p;
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • take a look at: https://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests – Ray Tayek Nov 14 '18 at 02:38

1 Answers1

1

Welcome to StackOverflow!

You should try combinations of valid and invalid input for all 3 parameters

1. t = E, n = 50000, p = 10
2. t = D, n = 50000, p = 10
3. t = A, n = 50000, p = 10
4. t = Z, n = 50000, p = 10
5. t = e, n = 50000, p = 10
6. t = D, n = 1000, p = 10
7. t = D, n = 10000, p = 10
8. t = D, n = 99999, p = 10
9. t = D, n = 100000, p = 10
10. t = D, n = 50000, p = 0
11. t = D, n = 50000, p = -10

Then you could also try the case where multiple inputs are invalid

12. t = Z, n = 1000, p = 10
13. t = D, n = 1000, p = -10
14. t = Z, n = 50000, p = -10
15. t = Z, n = 1000, p = -10

And many more. However, you don't have to cover every possible combination of inputs. Similar types of inputs can be represented with just 1 case (e.g. invalid uppercase letter can be represented with t = 'Z').

Andreas
  • 2,455
  • 10
  • 21
  • 24
  • Thank you so much for your response. I will try all these test cases. – Farah Mazin Nov 14 '18 at 03:45
  • I wrote the below test case to test the first test case. Is it correct?Am I missing something? – Farah Mazin Nov 14 '18 at 04:46
  • public void Testone() { try { new Cars('E', 50000, 10); // create an instance of class with correct values fail("Should have thrown an exception when One or more of the parameters have invalid value"); } catch (IllegalArgumentException e) { assertEquals("One or more of the parameters have invalid value", e.getLocalizedMessage()); // check message } catch (Exception e2) { fail("Caught exception but it is not IllegalArgumentException as expected");} – Farah Mazin Nov 14 '18 at 04:48
  • You shouldn't put the first `fail` there since all the parameters are valid. But yes, that is correct in case the one of the parameters is invalid – Andreas Nov 14 '18 at 05:47
  • Thank you! That helped a lot @Andreas Would you please tell me why is the below test case is passing without any errors even though I used the fail, and I used invalid p parameter. t=A, n=10000, p=-10 – Farah Mazin Nov 15 '18 at 01:33
  • Did t = D, n = 50000, p = -10 also pass without any errors? That might be something to do with how you write your unit test case – Andreas Nov 15 '18 at 01:36
  • Yes that case also passed. I used the same exact test unit above. – Farah Mazin Nov 15 '18 at 03:53
  • Yes, that should have passed since it is accepted by your `assertEquals` statement. – Andreas Nov 15 '18 at 04:20