Skip to content

Cascading mock does not work for non-public classes #603

@harish-prakash

Description

@harish-prakash

Cascading mock does not work for non-public classes

In a test class, I mocked a static factory and expected its methods to return a non-null mocked object. However, the result of the method was....

.... you guessed it, null.

Test Class

public class TheTestClass {

    @Tested
    TheClassUnderTest test;
    
    @Mocked
    TheStaticFactory mockTheStaticFactory;
    
    @Test
    public void theTestedMethod_doesNotReturnNull() {
        TheDependentService service = test.theTestedMethod();
        assertNotNull(service);
    }
}

Class Under Test

public class TheClassUnderTest {

    public TheDependentService theTestedMethod() {
        TheDependentService service = TheStaticFactory.getTheDependentService();
        return service;
    }
}

Static Factory

public class TheStaticFactory {

    public static TheDependentService getTheDependentService() {
        return new TheDependentService();
    }
}

The Dependent Service

class TheDependentService {
}

Note TheDependentService is not public

Other Information

JMockit Version 1.43
JDK Version 11

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions