Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.powermock.reflect.Whitebox.setInternalState(Object, Class<?>, Object) not working with object reference #971

Open
rea-al opened this issue Feb 11, 2019 · 1 comment

Comments

@rea-al
Copy link

rea-al commented Feb 11, 2019

PowerMock 1.7.4 should contain the ability of modifying a private static final field as stated by contributor on #673 but next issue is displayed:

org.powermock.reflect.exceptions.FieldNotFoundException: No instance field assignable from "org.apache.logging.log4j.Logger" could be found in the class hierarchy of Foo.

While running next test:

public class Foo {
    private static final Logger LOG = LogManager.getLogger(Foo.class.getName());
}

public class FooTest {
    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    Logger logger;

    @Test
    public void test() {
        Foo sut = new Foo();
        
        Whitebox.setInternalState(sut, Logger.class, logger);
    }
}

With the following environment:

  • OS: Windows 10 Enterprise x64
  • Java: Oracle jdk1.8.0_161 x64
  • Maven dependencies:
    • junit:junit:4.12
    • org.mockito:mockito-core:2.23.0
    • org.powermock:powermock-module-junit4:1.7.4
    • org.powermock:powermock-api-mockito2:1.7.4
@rea-al
Copy link
Author

rea-al commented Aug 15, 2019

Notice that if you replace Whitebox.setInternalState(sut, Logger.class, logger); for Whitebox.setInternalState(Foo.class, "LOG", logger); this works as expected.

@rea-al rea-al changed the title WitheBox.setInternalState on private static final field org.powermock.reflect.Whitebox.setInternalState(Object, Class<?>, Object) not working with object reference Aug 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant