• 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 ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

i need to extrend TestCase in order to use asserts of junit right

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am in a delema. i am told to use junit style tests with the asserts inside of an applet, like inside the code that makes an applet. the problem is java doesnt allow multiple inheritance and i cant also extend TestCase.

now i know how bad the design of putting assert statements inside an applets code smells design wise but its not my call.

i was wondering if there is an easy way to do this or do i have to remake junit asserts in a way. like make a assert object, that collects all these assert statements, and then have something run that object which will process the asserts and convert them to real junit asserts and obviously that object will extend TestCase.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea why you would want to do that - if you like, tell us more about it.

Anyway, all the assert methods are public static methods, so you don't need to inherit anything to be able to call them.
 
author & internet detective
Posts: 41124
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amaru,
In JUnit 4, you don't need to extend anything. I'd be more likely to write a normal test case though and call it's suite method from the applet. That way the applet runs the test but my test is logically separate.
 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
honestly i just want to be able to use the assertions and have a way to see if they are right. without changing methods to to test<method name> .

like lets say in my actionlistener i just want to assertNotNull("your top button is null",btnTop);

or if i just want to see if a string equals something

lets say in the init method i want to make sure a string is the right string.
String red="red";
assertEquals("your red string was set right", red,"red");

whats the easiest way to have these assert methods functioning in order to junit to find them. normally i was under the impresination you need to extend TestCase and start each method with test in order for junit to find it.

I was thinking about making an object that would have the assertmethods i need and just reverse engineering the junit methods (as in re writing the code behind the TestClass class) and embedding that object and making a static method that would run all the tests i populated it with.

I think there must be an easier way to do this care to elaborate? like i couldnt do it the regular way because i cant call my init testinit() for the applets sake. however couldnt i just store all these asserts in an object and then have a testSuite run them or find some other way to do things? thanks


like for this code below say that i just want to see if the color string is red. how would i put the assert in and what else would i have to do? thanks
public class HelloWorldSwing {

private static void createAndShowGUI() {
String color="red";
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);


frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, so you actually *don't* want to wrote JUnit style tests - you just want to add asserts to your production code?

In that case, you might actually be better of using vanilla Java asserts, assuming that you are using Java 1.4 or later.
 
Amaru Shakur
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou very much i was never aware hava had these asserts in it.) now i stored all of the AssertionErrors that could be generated by catching them after each assert in a list. Now i was just going to go through the list and print the stacktrace for each error i had in it. However i was thinking couldnt i throw these into junit so i could get the nice html.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you actually want to do? Now it sounds like you are actually more searching for something like a logging framework?
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic