Writing a Junit test for a boolean method with no parameters

Viewed 1059

I have a class where I want to write a junit test for. This method has no parameters, can this method accordingly?

public class classTobeTested {

    @Self
    SlingHttpServletRequest request;

    static final String keyword = "hello";

    public boolean isActive() {
        boolean check;
        String pathChecker;
        pathChecker = (request.getRequestURL()).toString();
        check= pathChecker.contains(keyword);

        return check;

    }

}

This would be the testing class i had in mind

@RunWith(MockitoJUnitRunner.class)
public class testclasstobetested {


    @Test
    public void TestclassTobeTested() throws Exception{
        classTobeTested  CTT = new classTobeTested();

        assertFalse(CTT.isActive("hello how are you"));
    }

}

I know my method does not take a parameter but has strings declared inside the method. How can i use assertFalse correctly to test a non param method.

1 Answers

Using annotations and Junit4 you can do it like this:

@RunWith(MockitoJUnitRunner.class)
public class testclasstobetested {

    @InjectMocks
    private classTobeTested CTT;

    @Mock
    private SlingHttpServletRequest request;

    @Test
    public void TestclassTobeTested() throws Exception{
        when(request.getRequestURL()).thenReturn(new StringBuffer("hello how are you"));

        assertFalse(CTT.isActive());
    }

}
Related