0

I am new for Junit's and i wrote Singleton class Junit Test case in my below code but i am getting exception and second method for loop not getting to cover can some one help me please to get it solve

Singleton

 public class MSA {
    private static final _msa = new MSA(); // TA is a singleton
    private ArrayList<MSThread> MSThreads = new ArrayList<MSThread>();

    private String msaInfo = null;

        public String getMsaInfo() {
            return msaInfo;
        }

        public void setMsaInfo(String msaInfo) {
            this.msaInfo = msaInfo;
        }

        private MSA() {}

        public static MSA getInstance() {
            return _msa;
        }

       public void shutdown() {
        for (MSThread t: MSThreads) {
            t.shutdown();
        }
    }

TestClass

  @Test
        public void setMsaInfoTest() throws Exception{
            MSA fixture = MSA.getInstance();
            fixture.setMsaInfo("sample");
            assertTrue(fixture.getMsaInfo() == "sample");
        }

  @Test
    public void testShutdown_1()
        throws Exception {      
        MSA fixture = MSA.getInstance();
        fixture.shutdown();
    }

Error

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at org.junit.Assert.assertTrue(Assert.java:52)
    at com.verizon.dtix.microsvcagent.MSATest.setMsaInfoTest(MSATest.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Krish
  • 4,166
  • 11
  • 58
  • 110
  • Where do you create an instance of MSA? Is it full code? – Khan Saab Jan 31 '20 at 13:31
  • i updated my code please check now – Krish Jan 31 '20 at 13:33
  • 3
    See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Don't use `==`. Or better yet, use `assertEquals` so that you get useful output when the test fails – byxor Jan 31 '20 at 13:34
  • no this is not answer to my question and i update my code – Krish Jan 31 '20 at 13:37
  • If you update the code, update the error message too. I'm almost certain that your test will pass now that you've applied our suggestions. – byxor Jan 31 '20 at 13:40
  • Also see [Should unit tests be written for getter and setters?](https://stackoverflow.com/questions/6197370/should-unit-tests-be-written-for-getter-and-setters). Most of the time, we can't extract very much value from testing getters and setters – byxor Jan 31 '20 at 13:41
  • Yes method1 passed now i need method2 for loop code coverage can some one suggest me please – Krish Jan 31 '20 at 13:45
  • You can test your loop by mocking `msThreads` and asserting that the `shutdown()` method was called on each one. See [mockito](https://site.mockito.org/#how), "now you can verify interactions". – byxor Jan 31 '20 at 13:50
  • 1
    Please don't modify your original post based on the answers received, it's really confusing. – Luis Iñesta Jan 31 '20 at 13:57
  • i am unable to access private array in test class,How can i cover it – Krish Jan 31 '20 at 13:58
  • Now problem is in my test class shutdown() method not cover fully,Can some one guide me to get it done – Krish Jan 31 '20 at 13:58
  • You can use dependency injection to access the list of threads. If you inject your list of threads as a constructor parameter (instead of building the list inside the class), you'll be able to mock your threads for the test. – byxor Jan 31 '20 at 16:07

1 Answers1

0

If you're using Maven, in pom.xml you can specify test classes that only run on a single thread. This is one of the easier and safer ways to run unit/integration tests with Singletons in Java. You can do this in the plugins section of the pom:

    <configuration>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
        <parallel>classes</parallel>
        <reuseForks>false</reuseForks>
        <parallel>none</parallel>
    </configuration>

     <executions>
        <execution>
            <id>single-thread-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>*/YourTestClassName.java</include>
                </includes>
                <threadCount>1</threadCount>
            </configuration>
        </execution>
    </executions>
djharten
  • 374
  • 1
  • 12