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);
}
Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.
Mockito 2 supports mocking of enum, but it is experimental feature. It needs to be enabled explicitly.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
Since CosProfileType
is declared public static
it is effectively a top level class (enum) so you could do
assertEquals("FRAME", CosProfileType.FRAME.name());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With