2

I'm trying to create some JUnit tests for my methods and I've been told to do it for every single method. I know it's a pitfall for beginners to only do positive tests so how can I test this method more than I already have? Also both of my test methods test the same thing essentially. What would be an example of a negative test here or is that redundant work?

public static MemberCont getInstance() {
    if(instance == null)  {
       instance = new MemberCont();
    }
    return instance;
}

@Test
public void getInstance() {
    assertNotNull(memberCo1.getInstance());
    assertEquals(true, memberCo1.getInstance() != null);
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
J.Kirk.
  • 943
  • 3
  • 12
  • 32
  • _"I'm trying to create some Junit tests for my methods and I've been told to do it for every single method."_ this is a bad advice, you should create unittests for every single **behavior**. – Timothy Truckle Oct 17 '16 at 11:30
  • The test you wrote is sufficient. There should be no other behaviors for the method you are testing - you will return the instance reference. – duffymo Oct 17 '16 at 11:31
  • your Class under Test implements the _"Java Singelton Pattern"_ which is also usually a misconception. – Timothy Truckle Oct 17 '16 at 11:32
  • Replace the thread-unsafe singleton pattern with a simple enum singleton definition. Also, the two asserts test basically the same thing, so you can remove the latter one. You might do a check if the reference you got returned is the same for two consecutive invocations to guarantee that no new object is created. – Roman Vottner Oct 17 '16 at 11:39

2 Answers2

2

You obviously try to implement the Singleton Pattern (which is not properly written BTW, check this), so the most important thing to test is that you will always get the exact same instance, so your test should rather be something like this:

@Test
public void getInstance() {
    assertNotNull(MemberCont.getInstance());
    // Make sure that we get the same instance between 2 subsequent calls
    assertSame(MemberCont.getInstance(), MemberCont.getInstance());
}

NB: It is a static method so you have no need to use an instance to call it, simply call it using the class name directly as I did above.

Community
  • 1
  • 1
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

UnitTests should always represent their structure which is 3 parts: arrange, act and assert. Also each desired behavior should have its own test. So your test should look lile this (based on answer of @NicolasFilotto):

@Test
public void getInstance__singleCall__NotNull() {
    // no "arrange" possible
    // act
    MemberCont cut = MemberCont.getInstance();
    // assert
    assertNotNull(cut);
}

@Test
public void getInstance__multipleCalls__alwaysTheSameObject() {
    // no "arrange possible
    MemberCont cut1 = MemberCont.getInstance();
    MemberCont cut2 = MemberCont.getInstance();
    assertTrue(cut1 == cut2);
}
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51