question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Private @Before* methods aren't called when defined in a base class

See original GitHub issue

TestNG Version

6.14.3 and 7.0.0-beta3, Java 8 and 11

Actual behavior

Private @Before* methods aren’t called when defined in a base class, but work when defined in the same class.

Expected behavior

Private @Before* methods should be called, whether they were inherited or not

Is the issue reproductible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

Following test passes - calls @BeforeMethod

public class NoInheritanceTest {
    boolean called = false;

    @BeforeMethod
    private void init() {
        called = true;
    }

    @Test
    public void testWorks() {
        assertTrue(called, "Should have been set in @Before");
    }
}

Following test fails - @BeforeMethod is not called:

public class ChildTest extends BaseTest {
    @Test
    public void testFails() {
        assertTrue(called, "Should have been set in @Before");
    }
}
public class BaseTest {
    boolean called = false;

    @BeforeMethod
    private void init() {
        called = true;
    }
}

Test case project: https://github.com/zhogov/testng-issue-2097

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Proryanatorcommented, Aug 21, 2019

Ah I see, those private methods I mentioned are just for methods in general.

Yeah you’re right, somewhere outside of there you’d need to also check if they’re TestNG annotated methods before showing the warning, good catch!

1reaction
juherrcommented, Aug 3, 2019

#473 has a similar title but looks a bit different.

@Proryanator At our side, only console warnings are possible but IDEs can manage a better display if they want to warn against this bad usage (ping @missedone @akozlova )

Read more comments on GitHub >

github_iconTop Results From Across the Web

private method in inheritance in Java - Stack Overflow
Private allows only the class containing the member to access that member. Protected allows the member to be accessed within the class and...
Read more >
The purpose of private methods and when to use them
Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external ......
Read more >
Inheritance - Learning the Java Language
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited...
Read more >
Private class features - JavaScript - MDN Web Docs
Private properties with the same name within different classes are entirely different and do not interoperate with each other. See them as external...
Read more >
private(call) and internal(call) - Discussion - Swift Forums
A method declared private(call) would behave exactly the same as a private method in almost all circumstances, the one exception being when a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found