• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

test app passing args

 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have created a small app. I need pass 1 param dinamically from the IDE .I need test with different values.


I am calling all my methods from "public static void main(String[] args) "

If I want send a param like "Scott". What way should I do it? I am using Netbeans

Automatically I have created a testClass

Like this




Maybe something like




In that code it doesn´t recognize Main.main(test[0]), there is a type problem
I never have test this. Any idea, please?

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete the keyword static throughout.
What are the annotations for? If you don’t know about passing arguments, how do you know about annotations?
What are you passing the arguments to? I can’t see anywhere you are passing them to.
I suggest you get a beginner’s book and read about objects and methods. Or the Java Tutorials. Give your class a field and methods which change that field. Then pass the arguments to those methods.
 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the anotations. The IDE created them.

I have an small app with a main method like



Then I need send a param dinamically to it and test it using different names for example "Scott".

I have create a test like



Any idea,, please?

I want run my app from the test

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of parameter does assertEquals() take? What does Main.main() return? Surely it is void?
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the IDE is creating code which you don’t understand, stop using it.
 
Isaac Ferguson
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

ok. I never have make this before and it is urgent for me. The unique thing I need is create a test case in JUnit for the next code


public static void main(String[] args) {

PreparedStatement ps = null;
Connection connection = null;
File filePerson = new File("C:\\dataFiles\\Person.data");
File fileOrder = new File("C:\\dataFiles\\Order.data");

String id = "";
ArrayList<String[]> ArrayPerson = readFile(filePerson);
ArrayList<String[]> ArrayOrder = readFile(fileOrder);
List<String> listId = new ArrayList<String>();

try {
connection = connectDB();

Statement sta = connection.createStatement();
ResultSet resultSetPerson = sta.executeQuery(
"select * from public.person");


int rsCountPerson = 0;
while (resultSetPerson.next()) {
rsCountPerson = rsCountPerson + 1;
}
ResultSet resultSetOrder = sta.executeQuery(
"select * from public.order");
int rsCountOrder = 0;
while (resultSetOrder.next()) {
rsCountOrder = rsCountOrder + 1;
}


insert(rsCountPerson, connection, ArrayPerson, filePerson);
insert(rsCountOrder, connection, ArrayOrder, fileOrder);


String sqlQueryPerson = "select LAST_NAME, FIRST_NAME from public.person,public.order where public.person.person_id=public.order.person_id";
ResultSet rs = fetchData(connection, sqlQueryPerson);

while (rs.next()) {

String last_name = rs.getString("last_name");
String first_name = rs.getString("first_name");
System.out.println("last_name" + last_name);
System.out.println("first_name" + first_name);
}
String sqlQueryOrder = "SELECT public.order.ORDER_ID,public.person.first_name,public.person.last_name FROM public.person INNER JOIN public.order ON public.person.person_id=public.order.person_id WHERE public.person.first_name='Frederic'";
rs = fetchData(connection, sqlQueryOrder);
while (rs.next()) {

String order_id = rs.getString("order_id");
String last_name = rs.getString("last_name");
String first_name = rs.getString("first_name");
System.out.println("----Order_Client-----");
System.out.println("last_name" + last_name);
System.out.println("first_name" + first_name);
System.out.println("order_id" + order_id);
}

} catch (ClassNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}

Also I want to improve aspect as

Java coding standards

usage of control statements(if,for,while, switch) Collection API

Utilization of OOP concepts

Eficiency of the code

Exception handling

Usage of design patterns

Test cases

Could you help to improve it please?



Any idea, please?

Thanks

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key point in using Unit Tests is to break your code down into individual, testable units. Putting all your code in public static void main() is not aligned with the goal of unit testing. Since it is void, you can not test its results via an expected value (the goal of assertEqual). The best you could do is create an array and assert exceptions do or don't happen when you expect or don't expect them.

Better: break your method into discreet units. Start with making the 'units' a method. Make the method take the parameters it needs to do a bit of work, and make it return the results of its work. Make tests that pass in know values as parameters, then get the return values and compare them against what you expect. Don't forget to test cases that should fail (and make sure the failure does happen and is expressed well).

Unit Testing is important, but is not something to learn before you know how to create methods and pass parameter. One step at a time... My suggestion would be to first get comfortable programming (using methods, Objects, classes, control statements, etc...). Only when you are comfortable with the language begin learning frameworks for testing (like JUnit).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic