0

Ive made a class that applies a tiling effect to a bufferedimage and i have to make a JUnit test on the class but im unsure of what to test and how i can test it. I know i have to use assert and the likes but im not sure how. I was thinking it would be something with the image being divided in 4.

public class TilesAction extends AbstractSelectedAction  { 
        public static String ID = "edit.Tiles";

    SVGImageFigure image;

    public TilesAction(DrawingEditor editor) {
        super(editor);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        final DrawingView view = getView();
        final LinkedList<Figure> figures = new LinkedList<Figure>(view.getSelectedFigures());
        final Figure figure = figures.getFirst();
        tiling(figure);
        fireUndoableEditHappened(new AbstractUndoableEdit() {
            @Override
            public String getPresentationName() {
                return labels.getTextProperty(ID);
            }

            @Override
            public void redo() throws CannotRedoException {
                super.redo();
            }

            @Override
            public void undo() throws CannotUndoException {
                super.undo();
            }
        });
    }        
    private void tiling(Figure figure) {
        image = (SVGImageFigure) figure;
        BufferedImage tilingImage = image.getBufferedImage();
         Graphics g = tilingImage.getGraphics();
         DefaultDrawingView ddv = new DefaultDrawingView();

    int width = tilingImage.getWidth() / 4;
    int height = tilingImage.getHeight() / 4;

    Image scaled = tilingImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    // Tile the image to fill our area.
    for (int x = 0; x < tilingImage.getWidth(); x += width) {
        for (int y = 0; y < tilingImage.getHeight(); y += height) {
            g.drawImage(scaled, x, y, null);
        }
    }
    g.dispose();

            }



}
A Sdi
  • 665
  • 2
  • 10
  • 24
MoeTheBro
  • 11
  • 3
  • 1
    To make your class better testable divide it into smaller testable classes or methods. For example, extract your anonymous to nested class (in eclipse put your cursor on "AbstractUndoableEdit", hit "Ctrl+1" and select "convert anonymous to nested class"). After that, you can move that nested class outside of your action (in eclipse put your cursor of the name of the newly created nested class, hit "Alt+Shift+T" and hit "V"). Now you can test your edit class separated. – Sergej Werfel Dec 02 '16 at 14:14
  • You can find some information how to write tests here https://github.com/junit-team/junit4/wiki/Getting-started But you should try to write your code testable first. So use small methods, few or none dependencies etc. – Sergej Werfel Dec 02 '16 at 14:15

0 Answers0