Skip to content

Instantly share code, notes, and snippets.

@gustavofranke
Created February 5, 2016 14:00
Show Gist options
  • Save gustavofranke/80b98d32852002cdf79a to your computer and use it in GitHub Desktop.
Save gustavofranke/80b98d32852002cdf79a to your computer and use it in GitHub Desktop.
Singleton with Enum in Java
import org.junit.Test;
import static org.junit.Assert.*;
public class MyTest {
@Test
public void shouldReturnASDF() {
assertEquals("asdf", MySingleton.INSTANCE.asdf());
}
@Test
public void shouldReturnQWER() {
assertEquals("qwer", MySingleton.INSTANCE.qwer());
}
}
enum MySingleton {
INSTANCE;
private String asdf;
private String qwer;
MySingleton(){
asdf = "asdf";
qwer = "qwer";
}
public String asdf(){
return asdf;
}
public String qwer(){
return qwer;
}
@Test
public void myTest() {
assertEquals("asdf", MySingleton.INSTANCE.asdf());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment