Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Testing main() / Hiding static method

Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My task is simple. I want my JUnit tests to test the main() method. The tricky part is that when there is an error that prevents the problem from continuing, it exits- naturally.

Luckily, the program is already designed to call a method that then exits- so for testing purposes, I thought I would create a subclass in the test case file that overrides that method with one that throws a RuntimeException instead of calling System.exit(). However, I'm having trouble.

This is a basic representation of the program:

The test file goes something like this:

BTW, yes, I have read the JavaRanch page on overriding vs hiding. Unfortunately, that doesn't seem to have helped. Every time I run the test, it never completes; when I debug, I find that it reaches the original class's System.exit() instead of the new method. I've tried passing in a stub class in ABC's application context file (both casting it as ABC or as ABCStub). I've tried putting ABCStub in a separate file. I've tried making ABCStub not a static class. I've been banging my head against the wall for hours and still haven't found a solution to this simple problem: hiding that static exit() method.
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. "Hiding" means that code compiled to use ABCTest will see ABCTest's exit method, but code compiled to use ABC's -- like ABC itself -- will continue to use ABC's, even if you call ABCTest.main(). That's just how it works. You're expecting some kind of polymorphism, but there isn't any -- that's the whole issue.

One solution: make ABC.exit() an instance method that calls. Make main() invoke exit() on a static protected member variable which holds an ABC by default. Then in ABCTest, override exit() and then set that member to hold an ABCTest.

There are other ways to do it, but they're all going to hinge on making exit() an instance method.
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic