In eclipse, how would I go about making a method call without writing a test class. For example, if I'm making a method that performs a certain algorithm, is it possible for me to simply call the method and pass the parameters in? I know the BlueJ IDE can do this so I'm sure Eclipse can, I'm just not sure how.
Thanks!
You need a main(String[] args) method to run anything in Java, and it needs to be in a class. Solution: create your test class. Welcome to real Java.
Related
What would be the correct way to verify that one behaviour is triggered when there is an argument being passed, and another behaviour is triggered when there are no arguments being passed when running a java app from cmd?
Since the main method is static it's a little tricky to verify, but I also feel that introducing PowerMock is a bit over the top just for that.
Basically I want to create an object with a constructor with no arguments if there are no cmd arguments, and create an object with a String argument constructor if there are passed arguments to the app.
I do not see your code, so I can only imagine how it looks like.
I can imagine that within the main method some logic is triggered, which results in one or another event.
I suggest thinking about moving the processing of the arguments to another class (ArgumentProcessor) which can be fed with a builder object or factory object in the constructor and it could have a process(String [] args) method that returns a runnable or whatever you want to achieve.
If you then feed the ArgumentProcessor with a stubbed builder/factory than I think it should be possible to check if the logic has been processed in the right way.
I know that I'll need a main method, but can that main method be in a different class other than the Main class?
Not all Java applications require a main method.
Java can also be used to create web applications, for instance, which don't require main methods to run.
The answer to your question depends on what exactly you mean. Do you mean a class with the name 'Main'? Then, no, there is no requirement for this at all.
The only requirement that Java has, is that the signature of the method is correct. the main method must:
be public
be main
be static
have returntype void
accept an array of Strings as (only) parameter
It's easier to add it in the public class in a file, but not mandatory. The name of the class it is in, is entirely up to you, though many will choose a name like 'Main' or 'Open', simply to more easily find it.
If you want to be able to run your application, by simple double-clicking the .jar file, you'll need to point to the class that contains the main method (to use: your application might contain a lot of main classes, used for internal testing, but only one can be used to start the actual application) in the manifest file: Manifest files
Prior to Java 7, it was possible to run a desktop application without a main method, by (ab)using an instantiation block, but this was removed as of Java 7, because this is not what the instantiation block was intended for.
It's not necessary to define yout main method in a main class. You can place your main method wherever you want, as long the syntax i correct :
public static void main (String[] args){
//...
}
You absolutely don't.
The method itself can be placed whereever you want it to be, there is no limitation.
However, I personally would recommend putting it in a class which at least contains something like "Main", because when others look at your code, and they are not using an IDE which supports jumping to the main method, people usually have an easier time finding your starting point.
However, that is just for sake of readability, and as I said, jumping to main is/should be usually a widespread supported feature
Yes, the Main method is required to run a function although a java class can be without the Main method. Though, it won't run...
I have a Java project(running in Eclipse) without main method and need to debug and see which is the caller class and the flow of the program. How do I start?
It is a simple project and does not contain any web/tomcat related data.
Thank you for your responses. I am new to StackOverflow and so pardon my writing and asking questions.
I am including packages and trying to create objects of a class, but it is not recognizing the classes. All the classes are public.
There is no way to run a Java SE application without starting with public static void main. If you want to debug the code of a library or framework you need to create a main method and call the code from there.
Take a look to JUnit. If you just want to debug your code is what you normally have to do.
Since Java requires all methods and variables to be within classes, the JVM needs a starting point that exists before any objects are initialized. Therefore, main must be static and public for the JVM to find it. Unlike C++, the main method does not return a status code, so it is of return type void rather than int.
OK, so before we start let me state that I have been googling and searching for an answer to my question for quite a while now and haven't been able to find a suitable one (the keywords are tricky since I keep getting unrelated posts and sites as results).
Now, moving on I have a Java class that contains a my main method and a number of other functions. I want to test these functions using JUnit but I can't instantiate a class that has main in it, if I simply try to call the function I get an error saying the function is outside the namespace even though both files are in the same package, and I get an error trying to import the file.
Is there anyway to test these functions using JUnit?
P.S. Yes I know that you can put them in a new class, but I don't think it is overkill to create a new class just for testing or to put in 2 functions that are for parsing user input, and there is still the issue of testing the main function itself (and it is not uncommon to write a main method just for testing).
So this is what happened. Since I don't use Java very often I ended up creating private data members in the class but treated them as I would globals in a C++ program. In consequence I initialized them in main and didn't think of making a constructor and hence the problem with instantiating the class. When that didn't work I tried the . form but since the methods referenced the private data members I would get an error without instantiating the class. Thanks to the guys that noticed the constructor thing.
You absolutely can create an instance of a class which contains a main method, so long as it has an accessible constructor of course.
Likewise you absolutely can call a static method directly, using MyClassName.myMethodName.
Having a main method in a class makes absolutely no difference to it in terms of the Java language itself - so you can test it just as you would any other class.
Very strange. I just wrote SomeClass with main inside and it's perfectly testable by SomeClassTest class.
Just a thought, did you declare constructors as private in the class with main method? It will help a lot if you can post some code snippet and exact error message you're getting.
In java I can simply test classes directly with
public static void main()
I then just add quick code and under Eclipse "Run" the class. Is there anything similar in C++ .NET ?
Right now I have to create an empty project, reference the correct headers then set that project as start up project.
Unit tests. There's also the object test bench if you're using VS.
I do this in C# so I don't know if this will react any differently, but I set up an empty "test" class with the main method and then set the project to startup with that class file. You shouldn't have to create the file in a separate project.
I'm not terribly familiar with Eclipse but if you're just looking to run your objects in the IDE do the following.
Open up the Immediate window and just call whatever function you want. This will start execute the code you type. You will likely have to qualify the name. Ex: ClassLibrary1.MyClass.SomeMethod()
I like using TestDriven.NET for this. It allows you to execute any public method by rightclicking on the header and selecting "Run test".
I like making
public static void Test()
methods on dialog- and form-classes to use with this feature.
It supports C++/CLI, so if that is what you mean by C++.NET, it should work for you.
Edit: You should only do that for things that are not automatically testable - such as pure GUI classes. Otherwise I agree with the other commenters: use a unit test framework.