• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to skip execution of a line in a method..need help ranchers  RSS feed

 
NarayanaRao Konakalla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I would like to get a clue about how to skip execution of a line of code in a method definition.Below is the scenario.

public PublicClass{

public void methodA(){

line1;
line2;
line3;

}
public static void main(String args){

PublicClass pc = new PublicClass();

pc.methodA();

}

}

That is the scenario.How could i see that line3 is not executed when the method methodA() is invoked in main method.
But, line3 should not be commented or any conditions should not be checked in methodA() definition.
Acutally this line3 has to be skipped as a part of testing.Hope i'll get help very soon.

Thanks
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

That request (If I have not misunderstood you) doesn't sound possible. Nor does it sound like object-oriented programming. A method has no way of knowing which other method has called it.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A possible solution would be to :
1. Refactor your code to make line 3 a method call

2. In your test, create a mock object of PublicClass which does nothing when otherMethod() is called
 
NarayanaRao Konakalla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Verre
Thanks for your immediate reply.
for creating mock objects, who provides the api?are there many implementations for this mock object api..?
if so, i would like to know if sun or apache is providing the api..

Thanks once again.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who provides the api?

Follow the link I have posted in my first reply. The API is provided by EasyMock (easymock.jar). EasyMock allows you to proxy some classes and add some custom behaviours to them. Like returning a custom value, throwing a custom exception...

are there many implementations for this mock object api..?

EasyMock is one of them. There are others, but I'm not familiar with any of them. You'll find a list here.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about some AOP?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain AOP; I had to google to find what it means.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well to explain AOP there is a lot of terminology.. concerns(some programming area; e.g. logging, authentication, transaction handling etc.), cross cutting concerns(concerns that cut across multiple
classes), join points(a well defined point of execution in java code e.g method), point cut(an AOP construct that picks a join point), advice (code (java) that executes at a joint point picked by a point cut) and more..

I'll try to give a brief idea based on AspectJ, which, is a java AOP implementation.

Lets say NarayanaRao's 3rd line is a method call to a method testMethod().
You write an Aspect (in AOP parlance a class like unit) and include a pointcut which picks the joinpoint testMethod() in NR's class. Then you write your testing java code within that advice. The pointcut can make the
context information from the join point available within the advice(for example method parameters).

Now armed with this Aspect you need to compile the class and the aspect with the AspectJ compiler "ajc". This is said to weave the Aspect code in to the byte code of the java class at the appropriate join point. Now you should be able run the compiled class.

You can manage your build process to have two separate build configurations which uses ajc and Aspects as the development build configuration, while, using javac in the production configuration. Such Aspects would be development Aspects (In contrast there are production Aspects). So NR's development cofiguration would execute the code in the Advice and the
production build will basically call an empty method.

Check the official FAQ here
[ November 04, 2008: Message edited by: Gamini Sirisena ]
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that EasyMock is some kind of AOP, because it proxies the target class and make some kind of AroundInvoke advice on any method of the target.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!