0

On my personal proyect, I want to make unit-tests but I'm having troubles with that.

My project does not have a big logic layer. The most important logic is the interaction between UI and entities. Some exception are calculate some day from payments and others things. Those things rarely change and, I want to start with things that change frecuently.

For example, please look this class:

public class TabClient extends JPanel{
  private JDateChooser dateChooser = ...

  private JButton update = ...
  private JButton search = ...
  private JButton delete = ...

  //other components specific for this panel/component

  private SomeOtherClassComponent subComponent = ...

  private void initComponents()
  {
     update.addActionListener(ClientHandler.getUpdateListener());
     //Others buttons
  }

  protected void mapFrom(Entitie entitie){
     subComponent.mapFrom(entitie);

     dateChooser.setDate(entitie.getDateFor...());
     //specific components mappings
  }

  protected void mapTo(Entitie entitie){
     subComponent.mapTo(entitie);

     entitie.setDateFor...(dateChooser.getDate());
     //specific components mappings
  }
}

This class is an example of a Tab (TabbedPane item) from my project.

The class ClientHandler is like a Mediator pattern who creates (and returns) EventListener to encapsulate the UI events. Some events call methos from Tab component like mapFrom

I want to write tests for the methods but I don't know where to start. If test with UI frameworks or refactor clasess to separate some things (what to separate? and where?) or what to do to start with unit-tests.

Nowdays, the test is made by hand using the app.

What I have to do to start with unit tests?

What I should test? Methods that interact with UI or what?

jotapdiez
  • 1,456
  • 13
  • 28

1 Answers1

0

In order to have an (easily) testable class, they need to have a high degree of cohesion, which may not be your case

You may want to consider moving any business logic to specific services or to the domain layer so that you can more easily test them.

As you mentioned the most import part of your application is the UI, I would try to mock the TabClient dependencies, simulate (say) the press of a button by calling its action methods and then checking the mocked objects to make sure the appropriate flow was invoked (ie, assert the right methods of the right objects were called). You may need to refactor your classes a bit to make possible to mock their dependencies. Likewise, I would write tests for all relevant UI objects.

I have never tested Swing applications to be honest, nevertheless I found the following links that might shed some light on your question (though some of them might require UI creation):

UISpec4J

Java World

Stack Overflow

Community
  • 1
  • 1