1

Below is code for which i'm trying to write text case and added what i did but getting null pointer exp

public boolean doVersionLimitCheck(Long mneId) throws DMMException {
    CALogUtil.getInstance().logMethodEntry("doVersionLimitCheck",
            ConfigArchiveManagerImpl.class.getName());
    boolean status = false;
    status = validateArchivedVersions(mneId);
    CALogUtil.getInstance().logDebug("Version Roll over status::" + status);
    CALogUtil.getInstance().logMethodExit("doVersionLimitCheck",
            ConfigArchiveManagerImpl.class.getName());
    return status;
}

for this i did like below.

@Test
public void testDoVersionLimitCheck() {

    Long mneId=Long.valueOf("123");
    ConfigArchiveManagerImpl impl = new ConfigArchiveManagerImpl();
    try {
        Mockito.doReturn(true).when(Mockito.mock(ConfigArchiveManagerImpl.class)).validateArchivedVersions(Mockito.anyLong());
    } catch (DMMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        impl.doVersionLimitCheck(mneId);
    } catch (DMMException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
mae
  • 117
  • 2
  • 12
  • 1
    Use dependency injection. Instead of a private method `validateArchivedVersions`, you pass the method (or class constructor) some object which has a public method of the same name. You can then mock that object. If you mock the internal behaviour of methods, you're no longer testing anything. – Michael Jan 18 '19 at 14:23
  • validateArchivedVersions is publc method only – mae Jan 18 '19 at 14:25
  • Mocking methods does not work that way with Mockito - it only modifies the mock instance passed to `when`, not the "real" `impl` one you are calling `doVersionLimitCheck` on. – Hulk Jan 18 '19 at 14:26
  • 1
    Invoking public methods of the same class in a public method cause often this kind of issue during unit testing. I see 2 decent choices : move it to another class as suggested by Michael or don't mock it. – davidxxx Jan 18 '19 at 14:27
  • could please provide me the solution – mae Jan 18 '19 at 14:27
  • Possible duplicate of [Use Mockito to mock some methods but not others](https://stackoverflow.com/questions/14970516/use-mockito-to-mock-some-methods-but-not-others) – Hulk Jan 18 '19 at 14:28
  • what if validateArchivedVersions is private method – mae Jan 18 '19 at 14:29
  • @mae then you can't mock it with Mockito... in that case, you need more powerful/invasive reflection - PowerMockito may be an option, see https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using-powermock – Hulk Jan 18 '19 at 14:31
  • If it is private you don't want to mock it generally. Show the code of `validateArchivedVersions()` and we could propose a way to deal with. – davidxxx Jan 18 '19 at 14:31
  • @davidxxx It is very big ther ,that again calling some more functionality inside that's y trying to mock. – mae Jan 18 '19 at 14:38
  • 1
    @mae especially if it is big, it's probably dangerous to just mock it - perhaps it has some side-effect that's important for the overall functionality of the class? It may be better to try to inject some kind of `MockArchive` to this `ConfigArchiveManagerImpl` class if you want to avoid interacting with real ressources. (assuming this ArchiveManager actually manages an archive, that is...) – Hulk Jan 18 '19 at 14:42
  • MockArchive ..? – mae Jan 18 '19 at 14:47
  • I have same kind of issue but here internally called method is private – mae Jan 18 '19 at 14:54
  • Could you please give me suggestion to mock final class static methods – mae Jan 18 '19 at 15:19

1 Answers1

1

You need to spy on the SUT in order to test one method and mock the other:

@Test
public void testDoVersionLimitCheck() {

    Long mneId=Long.valueOf("123");
    ConfigArchiveManagerImpl impl = Mockito.spy(new ConfigArchiveManagerImpl());
    try {
        Mockito.doReturn(true).when(impl ).validateArchivedVersions(Mockito.anyLong());
    } catch (DMMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63