Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:46:32+00:00 2026-06-07T08:46:32+00:00

I am used to JUnit running @Before methods in a superclass before @Before methods

  • 0

I am used to JUnit running @Before methods in a superclass before @Before methods in a subclass. However, I’ve got a Groovy test class which inherits from another Groovy class, and they both contain @Before methods; the problem I have is that @Before method in the test class is running before the one in its superclass, and I’m getting NPEs from uninitialised variables (the superclass is supposed to take care of that).

The superclass is something like this:

import groovyx.net.http.RESTClient
import org.junit.Before

abstract class BaseTestClass {
    def client

    @Before
    void setUp() {
        client = new RESTClient()
    }
}

And the subclass is something like this:

import org.junit.Before

class TestClass extends BaseTestClass {

    @Before
    void setUp() {
        client.post(path: '/entity', body: '{"id":"test"}')
    }

    ...
}

This is a simplified version, but I get the error: java.lang.NullPointerException: Cannot invoke method post() on null object in the setUp() method of the subclass. Any ideas what could cause this behaviour? All the docs say it should be the other way round, and I’ve never previously experienced any different.

I’m running using the maven-failsafe-plugin, junit-4.10 and jdk-1.6.0_31. Interestingly, I have other Groovy test classes in the same place (same package, same project, same directory) which do not suffer from this problem – the ordering of @Before methods is correct; furthermore, it appears to be deterministic – it’s always the same test class that has the problem.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:46:34+00:00Added an answer on June 7, 2026 at 8:46 am

    You named both of your @Before methods setUp(). The child’s setUp() method overrides the parent’s one. So the line client = new RESTClient() is never called.

    Just pick another name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand in Junit 4, @Before can be used to setup test fixtures for
I have a method which is used to check if the given class is
I'm trying to run Junit test from my ant build.xml file. I read here
I have a Java class for use with JUnit 4.x. Within each @Test method
I just used MyEclipse to automatically generate some JUnit test cases. One of the
I'm new to concurrency in Java. I've used Junit to test some of my
I am writing (junit) unit tests for a class which implements an exposed interface
I have a class called Menu.java which is used as the interface for my
I have a class where Runtime.getRuntime() is used for executing a script from command
Having used storyboards for a while now I have found them extremely useful however,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.