How to test anonymous classes?

I believe you should be familiar with this idiom, which is some kind of Java excuse for closing

//In the "Resource Manager" class
public void process(Command cmd){
  //Initialize
  ExpensiveResource resource = new ExpensiveResource();
  //Use
  cmd.execute(resource);
  //Release / Close
  resource.close();
}

//In the Client class...
manager.process(new Command(){

  public void execute(ExpensiveResource res){
    //Do things with the resource
  }
});

I used this idiom / template a lot, but recently I tried to test it and it gave me a headache ...

How can you test the isolated class ResourceManager and Client? I have found that it is so closely related to them that you cannot do it easily.

Ideas appreciated.

Hello

+3
source share
3 answers

, , , execute() , . (http://xunitpatterns.com/Humble%20Object.html).

.

, #, -. , , , .

, Javascript, - . - .

+1

, , , / , .

- , , , , .

+5

( ). , , .

, , OO ( ).

, , - .

0

Source: https://habr.com/ru/post/1712905/


All Articles