yesco1
yesco1

Reputation: 391

How to test ENUMs using JUNIT

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);

}

Upvotes: 12

Views: 73673

Answers (3)

Rohit Raghuvanshi
Rohit Raghuvanshi

Reputation: 59

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());

Upvotes: 1

Reimeus
Reimeus

Reputation: 159754

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

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

Upvotes: 22

jbarrueta
jbarrueta

Reputation: 5175

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());

}

Upvotes: 3

Related Questions