3

I try to write test then it use static class.

I have class Person:

public class Person {
  private int id;
  public Person(String name) {
    id = Id.generate(name);
  }
  public int getId() {
    return id;
  }
}

And class Id:

public class Id {
  public static int generate(String name) {
    if (name.equals("I")) {
      return 42;
    }
    return 100;
  }
}

This is my test:

import org.easymock.EasyMock;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.testng.Assert;
import org.testng.annotations.Test;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { Id.class } )
public class MockStaticTest {

  @Test
  public void test() throws Exception {
    PowerMock.mockStatic(Id.class);
    EasyMock.expect(Id.generate(null)).andReturn(55);

    PowerMock.replayAll();
    Person person = new Person(null);
    Assert.assertEquals(person.getId(), 55);
    PowerMock.verifyAll();
  }
}

When I run test, it throws NPE:

java.lang.NullPointerException
    at Id.generate(Id.java:3)
    at MockStaticTest.test(MockStaticTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...

On line 17 it calls code of static method Id.generate. In my opinion, it is wrong behavior.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Radoslav
  • 85
  • 2
  • 9

3 Answers3

2

Calling new Person(null) will throw this exception as the constructor calls generate(name) and generate calls name.equals. That is your problem: name is null.

One way round this in your case would be to use the Yoda condition "I".equals(name). Although this looks odd at first glance, it does allow you to rely on java.lang.String making a null check in .equals, meaning you don't have to do it yourself.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I know that NPE throw `name.equals("I")`, it is the intention. This is proof that `EasyMock.expect(Id.generate(null)).andReturn(55);` call `Id.generate(null)`. In my opinion, this is wrong behavior. – Radoslav Dec 14 '15 at 13:36
0

You are passing null as an argument, then why should it not give a NullPointerException? At line 17 you should give a proper argument.

sschrass
  • 7,014
  • 6
  • 43
  • 62
pratapvaibhav19
  • 222
  • 2
  • 12
0

I found solution. Test class must extend PowerMockTestCase.

Correctly:

public class MockStaticTest extends PowerMockTestCase {
...
}
Radoslav
  • 85
  • 2
  • 9