create a test class (main method) to start(run) the class in Java
Maybe I do not understand something.
So I've created the class in Java, which does some stuff.I need to create a main method or mainClass, that will start(run) my class. (I do not mean JUnit).
Question: how to do it?
is there some sort of methodology?
described in the books somewhere?
Help me please
Re: create a test class (main method) to start(run) the class in Java
You should create another class that consists of the main class.
Code :
package myproject;
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Was that helpfull?I mean the structure is like this.I use netbeans IDE.There all i do is to right click on the project ,select new JAVA class,write the class and then creating an object of her for instance in main
Re: create a test class (main method) to start(run) the class in Java
Yes,I agree.but what if it is something complicated?
like this
Code :
package handler;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class TextHandler extends Handler {
private ArrayList<String> fileTypes = new ArrayList<String>();
private String extension;
private String data;
public TextHandler(Handler next) {
super(next);
fileTypes.add("html");
fileTypes.add("htm");
fileTypes.add("css");
fileTypes.add("js");
}
public Response handle(Request1 recvest){
String uri=recvest.getParameter("URI");
if (uri != null) {
String extension = uri.substring(uri.lastIndexOf('.')+ 1);
}else{
return super.handle(recvest);
}
if (!fileTypes.contains(extension)){
return super.handle(recvest);
}
String webRoot = Configurator.getInstance().getSettingsParameter("web-root");
String filePath=webRoot+uri;
File file = new File(filePath);
if(!file.exists()){
super.handle(recvest);
}
try{
FileInputStream fis = new FileInputStream(file);
DataInputStream din=new DataInputStream(fis);
byte[] bytes;
int length= din.read(bytes);
String data=new String(bytes,0,length);
din.close();
fis.close();
}catch (FileNotFoundException fnfex) {
super.handle(recvest);
} catch(IOException ioex){
super.handle(recvest);
}
String[] ParamArray = {"HTTP/","Server","ContentType","Content-Length","Content-Type","Connection"};
String totalResponse = ParamArray[0]+"1.1"+"200 OK"+"/n"+
ParamArray[1]+"YuriyServer/2009-09-09 "+"/n"+
ParamArray[2]+(int)file.length()+"/n"+
ParamArray[3]+"text"+extension+"/n"+
ParamArray[4]+"close"+"/n";
ResponsBuilder responsBuilder=new ResponsBuilder();
responsBuilder.buildRespons(totalResponse.getBytes());
responsBuilder.setData(data);
}
}
Re: create a test class (main method) to start(run) the class in Java
Quote:
Originally Posted by
curious725
...what if it is something ...like this
Well, before you began writing that TextHandler class, you had a plan to test it, right? I mean you must have had something in mind.
So...
In a brand new file, create a public class with a main() function that does whatever it is that you had in mind.
Maybe something like:
Code java:
//
// File Z.java or TestTextHandler.java or whatever name you want for this test class
//
// May have import statements...
//
public class Z // Or TestTextHandler or whatever name you want for this test class
{
public static void main(String [] args)
{
// May have "local" variables used in main()
// Create a TextHandler object and whatever
// other objects you are going to access from
// main()
// Do stuff with the TextHandler object and whatever
// other things you need to deal with.
} // End of main()
// May have static data members and methods and inner
// classes, etc., etc., etc. used in this test class
} // End class definition
Cheers!
Z
Re: create a test class (main method) to start(run) the class in Java
Re: create a test class (main method) to start(run) the class in Java
Your answer is better.Thank you