-2

How many tests could you write for the enum class below. I am looking for the following O/P.

given the following command

java fileName HORIZON_BOX, HORIZON_BOX_WITH_CC,HORIZON_BOX_WITH_CC

1 HORIZON_BOX: 20.00
2 HORIZON_BOX_WITH_CC @ 30.00 : 60.00
GRAND TOTAL : 80.00

What is the best solution approach that you would go about solving this problem?

public enum Product {

    HORIZON_BOX(30.00),
    HORIZON_BOX_WITH_CC(50.00),
    HORIZON_BOX_WITH_CC_2_TB(100.00),
    HORIZON_MULTIROOM(75.00),
    HUB(20.00);

    private double price;

    private Product(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

}
Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
Angelo
  • 1
  • What have you tried so far? Looks like a home assignment for which you are expecting a full answer without having tried anything :) – Bastien Jansen Mar 20 '13 at 14:01
  • I have written all the code. I just want to better myself and would like suggestions. As I am new on the forum I do not know where I can show all the code I have done. – Angelo Mar 20 '13 at 15:11
  • Either paste it in your question, of if it's too long you can create a [gist](https://gist.github.com) and link it here. – Bastien Jansen Mar 20 '13 at 15:12
  • If you would like to see the code I have it here http://codereview.stackexchange.com/questions/24161/tdd-approach-and-simple-solution – Angelo Mar 20 '13 at 15:23
  • Thank you. I am learning so much from you guys. – Angelo Mar 20 '13 at 15:27

1 Answers1

2

I think that the only tests which would make sense for this enum would be to create an instance of each value and check if the getPrice() method returns the expected price.

Regarding the arithmetics you do with the enum: That's a different question which can't be answered without seeing your actual source for doing that and your requirements. The usual thought process when designing unit tests is:

  • What are normal use-cases according to my specifications?
  • What are rare edge cases which might be tricky to handle or require special logic (like minimum or maximum allowed values)?
  • What are use-cases outside of the specification which should result in an error?
  • Are all possible code paths tested?

By the way: You shouldn't use double to represent monetary values. You should use BigDecimal, because it allows you to specify the number of decimal places and the rounding strategy. Normal double arithmetics do not follow the rounding rules which are customary in financial transactions. See this question for details.

Community
  • 1
  • 1
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • I have tested all the enums and the getPrice method. I also used a HashMap and switch structure to get the intended output. I would like to post the code I have and get some candid feedback – Angelo Mar 20 '13 at 15:13
  • Thanks very much Philipp. What is this site for? Just answers or can I also smaller code here. I have posted my question here. http://codereview.stackexchange.com/questions/24161/tdd-approach-and-simple-solution – Angelo Mar 20 '13 at 15:23
  • When you want to know the scope of a site of the stackexchange.com network, please read the [FAQ](http://codereview.stackexchange.com/faq) of that site. – Philipp Mar 20 '13 at 15:54