Skip to content

Mocking static methods and instances from the same class doesn't work with TestNG #82

@helmbold

Description

@helmbold

The following example doesn't work with TestNG 6.8.8, JMockit 1.12 and Java 1.8.0_20.

public class DateUser {

  public static boolean isLunchTime() {
    int hour = LocalDateTime.now().getHour();
    return hour >= 12 && hour <= 14;
  }
}

public class DateUserTest {

  @Test
  public void shouldReturnTrueFor12h(@Mocked LocalDateTime dateTime) {
    new Expectations() {{
      LocalDateTime.now(); result = dateTime;
      dateTime.getHour(); result = 12;
    }};

    boolean isLunchTime =  DateUser.isLunchTime();

    assertThat(isLunchTime).isTrue();
  }
}

It works only with JUnit.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions