-1

I am writing junit test cases and using ECLEMMA for checking unit test coverage. I have following code in ServerClass.class This class is setting the status of a server.

         public class ServerClass{
         private boolean isStarted;
         public static final String MESSAGE_START = "Started";

         private void setStarted( boolean isStarted ) {
            this.isStarted = isStarted;
           }
         public String start() {
             setStarted( true );
             return ServerClass.MESSAGE_START;
           }
       }

I have a test case in my test class:

    @Test
     public void startTest(){
        ServerClass serverClass = new serverClass ();       
        assert("Started".equals( serverClass. start() )); // 3 of 4 branches missed
}

In eclipse, after running eclemma, I am getting a yellow dot in assert statement and code coverage is low. Please help me in understanding the coverage logic and solution.

technicalworm
  • 171
  • 1
  • 2
  • 10

2 Answers2

3

The JUnit method to check that something is true is named assertTrue(), not assert(). assert() is a native Java assertion, that won't be executed if assertions are not enabled.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

Try to add a test for setStarted:

@Test
public void setStarted(){
    ServerClass serverClass = new serverClass ();       
    serverClass.setStarted(false);
    //assert a getter for isStarted (not in your example)
    assert (serverClass.isStarted() == false);
}
JFPicard
  • 5,029
  • 3
  • 19
  • 43