mloayzagahona / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

mockStatic of JAXBContext fail with PowerMockRule #477

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. this is the class that i`m testing:
public class Some {
  public String some() {
    return String.format("123", "456");
  }

  public JAXBContext getJAXBContext() throws JAXBException {
    return JAXBContext.newInstance(Configuration.class);
  }
}

2. this is my test class
@PrepareForTest({Some.class, JAXBContext.class})
//@RunWith(PowerMockRunner.class)
public class SomeTest {

  @Rule
  public PowerMockRule rule = new PowerMockRule();

  @Test
  public void testSome() {
    PowerMockito.mockStatic(String.class);
    PowerMockito.when(String.format("123", "456")).thenReturn("789");
    Some s = new Some();
    String value = s.some();
    Assert.assertEquals("789", value);
  }

  @Test
  public void testJAXB() throws JAXBException {
    PowerMockito.mockStatic(JAXBContext.class);
    JAXBContext mock = PowerMockito.mock(JAXBContext.class);
     PowerMockito.when(JAXBContext.newInstance(Configuration.class)).thenReturn(mock);

    Some some = new Some();
    JAXBContext context = some.getJAXBContext();
    Assert.assertSame(mock, context);
  } 
}
3. using maven with the following dependencies:
<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-module-junit4-rule-agent</artifactId>
   <version>${powermock.version}</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.powermock</groupId>
   <artifactId>powermock-api-mockito</artifactId>
   <version>${powermock.version}</version>
   <scope>test</scope>
</dependency>

with plugin configuration:
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <argLine>-javaagent:${org.powermock:powermock-module-javaagent:jar}</argLine>
      <useSystemClassloader>true</useSystemClassloader>
    </configuration>
</plugin>

What is the expected output? What do you see instead?
getting the error from the test:
Tests in error:
  testJ(com.att.tlv.ci.crawler.test.parser.SomeTest):
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.

What version of the product are you using? On what operating system?
<powermock.version>1.5.2</powermock.version>
java version "1.6.0_30"

Please provide any additional information below.
when i put PowerMockRunner (commented out in my example) it work fine.
only with the javaagent it fails..

Original issue reported on code.google.com by 88ch...@gmail.com on 14 Jan 2014 at 1:52

GoogleCodeExporter commented 9 years ago
forgot to mention that only the second test fail (the one with the JAXB), the 
first test (with the String.format) in both cases pass.

Original comment by 88ch...@gmail.com on 14 Jan 2014 at 1:54