12

How can I create test cases using JUNIT to test ENUMS types. Below I added my code with the enum type.

public class TrafficProfileExtension { public static enum CosProfileType { BENIGN ("BENIGN"), CUSTOMER ("CUSTOMER"), FRAME ("FRAME"), PPCO ("PPCO"), STANDARD ("STANDARD"), W_RED ("W-RED"), LEGACY("LEGACY"), OPTIONB ("OPTIONB"); private final String cosProfileType; private CosProfileType(String s) { cosProfileType = s; } public boolean equalsName(String otherName){ return (otherName == null)? false:cosProfileType.equals(otherName); } public String toString(){ return cosProfileType; } } } 

I created a test case for my enum CosProfileType, and I am getting an error on CosProfileType.How can I make this test case work?

@Test public void testAdd() { TrafficProfileExtension ext = new TrafficProfileExtension(); assertEquals("FRAME", ext.CosProfileType.FRAME); } 
3
  • 2
    What functionality are you trying to test? Commented Aug 21, 2015 at 22:33
  • You are asserting that a String is equal to an enum instance. What is ext? Commented Aug 21, 2015 at 22:33
  • I am getting an error on CosProfileType :| Commented Aug 21, 2015 at 22:34

3 Answers 3

22

Since CosProfileType is declared public static it is effectively a top level class (enum) so you could do

assertEquals("FRAME", CosProfileType.FRAME.name()); 
Sign up to request clarification or add additional context in comments.

1 Comment

Worth noting that an enum is always a static nested class - it cannot be an inner class. The static modifier is redundant in this case...
3

You are comparing and String to an Enum that will never be equal.

Try:

@Test public void testAdd() { TrafficProfileExtension ext = new TrafficProfileExtension(); assertEquals("FRAME", ext.CosProfileType.FRAME.toString()); } 

1 Comment

Try assertEquals("FRAME", CosProfileType.FRAME.toString());
1
assertEquals("FRAME", CosProfileType.FRAME.name()); 

It will work only when field and value both are same but won't wotk for below:

FRAME ("frame_value") 

Better to check with

assertEquals("FRAME", CosProfileType.FRAME.getFieldName()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.