Testcases implementation for Java similar to topcoder/codecheff - java

I wanted to implement a testcase which will run particular java class and then, provide input to it and takes output from that.
So, there are three questions here:
1) How do we run another java class from junit? Do we need to run command line to do this?
2) After java class is run, how do we provide input to it? Do we print that on console?
3) How to read output of program, do we read console here?
I haven't written many test cases, written simple test cases which will call a method in any java class and asserts that. Can anyone help me in doing this?
Note: this is neither a college/school home work nor this is related to company work...... :p

1) Testing code written in another class from a junit test method involves either creating an instance of that class and then calling its methods, or calling static methods of the class. If you want to run that class's main method (the method that gets run automatically when you run your java program from the command line), then just call MyClass.main(someArgs), where someArgs is the fake commandline arguments given as an array of Strings.
2) After main is called your program will run like normal. You can enter your input manually to test your program if you want. But since you are writing test methods you probably want to automate this...
3) You can use System.setIn and System.setOut to redirect input and output to your own InputStream and OutputStream. Then you can send "user" input straight from your test method, and make assertions based on the content of the OutputStream.

Related

Verify behaviour based on app argument JUnit

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.

How to run MapReduceIndexerTool job from Java?

I want to run MapReduceIndexerTool from Java.
Right now I do it from command line using hadoop jar as you can see here, but I want to check it's status (to see if it's finalized, in progress, etc.) from Java code.
So basically I want to run it from Java in order to be able to check it's status from Java. Is there a way to run it from command line and check it's status from Java?
Also, there is a way to make Map Reduce to send an event (on a callback for example) when a job is completed? Something like a webhook?
As far as I know Tool interface exposes only int run(String[] args) method, so in general you would create new instance, form proper argument string and call that method.
From other hand, MapReduceIndexerTool has int run(Options options) method, that could be used to run it without forming shell-style argument. However, this method is protected, so this will need to have calling class to be created in same package as MapReduceIndexerTool.

How can i use redefineClasses() method in javaagents

I have been using premain() with addTransformer(). Since, it gives javassist.ClassNotFound exceptions for certain classes when i run the agent with a server, i thought to try the agentMain() with redefineClasses(). I went through many links, but so far i am unable to find a piece of code that gives me clear idea on how to set up a simple java agent using these two methods. Some help would be really appreciated.
Can we use redefineClasses() with premain()? (When we use redefineClasses() do we still need the transform method?)
I am trying to instrument set of methods of set of classes, where i know the fully qualified name of those classes as com.test.Foo. I wanted to instrument them without going through the entire set of classes loaded onto JVM. I have been reading those documents back and forth, but still i am unable to get a clear idea on how to use that redefineClasses method?
You can call redefineClasses from anywhere, also from a premain method which is nothing but an extension to a normal Java program run by the same JVM process previous to a main method.
A trivial example for running a redefinition is:
instrumentation.redefineClasses(new ClassDefinition(Foo.class, new byte[] {...}));
This way, Foo is set to be represented by the byte array that must contain a valid class file for Foo where all signatures of fields and methods are the same as by the loaded Foo.class. You can use a tool like ASM for instrumenting the class.
If you really only want to instrument Foo, then this might just be the way to go instead of using a ClassFileTransformer.

Is there anyway to mimic or mock a Linux command line in a Java JUnit test

I have been working recently on a JUnit test for an application.
This application is executed in a Linux environment and the arguments, options and other critical information is incorporated into the command line which is then sent to the Java code.
I have been trying to find different ways to test certain aspects of the application using JUnit 3.8.1, but I have run across some issues with methods that deal with the command line and arguments passed from it.
My question: Is there anyway to set up a mock or psuedo Linux command line prompt or even try hard code a command line that is fed to the test methods?
I have been researching for a while but I can't find anything that really answers my question.
Any help would be greatly appreciated.
The main method is a method like any other, so you can unit test it like any other, passing through an array od strings as the command line arguments.
But I don't do this. Instead, I have the main method create a Program object and delegate to that object. I unit test this Program class. This is so I can mock other aspects of the environment: the constructor for the Program class is passed a string array for the command line arguments, a Properties object for the system properties, a Map for the environment variables, and so on. I'm left with a small main method that I don't unit test, and a Program class I can thoroughly unit test.
I don't know if ist is that what you search. Bit to execute commando linea commands from java, you can use ExecuteShellCommand.
ExecuteShellComand obj = new ExecuteShellComand();  
String output = obj.executeCommand(command);

Make single method call without class from Eclipse

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.

Categories

Resources