Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Unit Testing in Java What to Test Rules rule!

Amitkumar Patel
Amitkumar Patel
6,699 Points

constructingLargerThanAlphabetNotAllowed test. It currently throws an exception. How to write the catch statement?

I'm unsure of what to do here:

com/teamtreehouse/vending/AlphaNumericChooserTest.java
package com.teamtreehouse.vending;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;


import static org.junit.Assert.*;

public class AlphaNumericChooserTest {

    private AlphaNumericChooser chooser;

    // I added this rule setup code for you, thank me later. ;)
    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void setUp() throws Exception {
        chooser = new AlphaNumericChooser(26, 10);
    }

    @Test
    public void validInputReturnsProperLocation() throws Exception {
        AlphaNumericChooser.Location loc = chooser.locationFromInput("B4");

        assertEquals("proper row", 1, loc.getRow());
        assertEquals("proper column", 3, loc.getColumn());
    }

    @Test(expected = InvalidLocationException.class)
    public void choosingWrongInputIsNotAllowed() throws Exception {
        chooser.locationFromInput("WRONG");
    }

    @Test(expected = InvalidLocationException.class)
    public void choosingLargerThanMaxIsNotAllowed() throws Exception {
        chooser.locationFromInput("B52");
    }

    @Test(expected = InvalidLocationException.class)
    public void constructingLargerThanAlphabetNotAllowed() throws Exception {
         chooser.locationFromInput("27, 10");
    }
}
com/teamtreehouse/vending/AlphaNumericChooser.java
package com.teamtreehouse.vending;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AlphaNumericChooser extends AbstractChooser {
    private int ALPHA_FIRST = (int) 'A';
    private int ALPHA_LAST = (int) 'Z';

    public AlphaNumericChooser(int maxRows, int maxColumns) {
        super(maxRows, maxColumns);
        int maxAllowedAlpha = (ALPHA_LAST - ALPHA_FIRST) + 1;
        if (maxRows > maxAllowedAlpha) {
            throw new IllegalArgumentException("Maximum rows supported is " + maxAllowedAlpha);
        }
    }

    @Override
    public Location locationFromInput(String input) throws InvalidLocationException {
        Pattern pattern = Pattern.compile("^(?<row>[a-zA-Z]{1})(?<column>[0-9]+)$");
        Matcher matcher = pattern.matcher(input);
        if (!matcher.matches()) {
            throw new InvalidLocationException("Invalid buttons");
        }
        int row = inputAsRow(matcher.group("row"));
        int column = inputAsColumn(matcher.group("column"));
        return new Location(row, column);

    }

    private int inputAsRow(String rowInput) {
        rowInput = rowInput.toUpperCase();
        return (int) rowInput.charAt(0) - ALPHA_FIRST;

    }

    private int inputAsColumn(String columnInput) {
        int columnAsInt = Integer.parseInt(columnInput);
        return columnAsInt - 1;
    }
}
com/teamtreehouse/vending/AbstractChooser.java
package com.teamtreehouse.vending;

public abstract class AbstractChooser {
    private final int maxRows;
    private final int maxColumns;

    public class Location {
        private final int row;
        private final int column;

        protected Location(int row, int column) throws InvalidLocationException {
            if (row > maxRows || column > maxColumns) {
                throw new InvalidLocationException("Invalid Location");
            }
            this.row = row;
            this.column = column;
        }

        public int getRow() {
            return row;
        }

        public int getColumn() {
            return column;
        }
    }

    public AbstractChooser(int maxRows, int maxColumns) {
        this.maxRows = maxRows;
        this.maxColumns = maxColumns;
    }

    public abstract Location locationFromInput(String input) throws InvalidLocationException;
}
com/teamtreehouse/vending/InvalidLocationException.java
package com.teamtreehouse.vending;

public class InvalidLocationException extends Exception {
    public InvalidLocationException(String s) {
        super(s);
    }
}
Amitkumar Patel
Amitkumar Patel
6,699 Points

It says: but I'd like you to use it in the constructingLargerThanAlphabetNotAllowed test. It currently throws an exception. Can you use thrown rule I created for you to make sure we are getting the expected message from the assertion?

So in the first screenshot I have this for the answer: @Test(expected = InvalidLocationException.class) public void constructingLargerThanAlphabetNotAllowed() throws Exception { chooser.locationFromInput("27, 10");

I'm not sure how to change this to answer the question?

Any assistance would be very much appreciated.

2 Answers

Ivailo Hristov
Ivailo Hristov
6,297 Points

Write

@Test public void constructingLargerThanAlphabetNotAllowed() throws Exception { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Maximum rows supported is 26"); chooser.locationFromInput("27, 10"); }

Hi

Currently you have this:

    protected Location(int row, int column) throws InvalidLocationException {
        if (row > maxRows || column > maxColumns) {
            throw new InvalidLocationException("Invalid Location");
        }
        this.row = row;
        this.column = column;
    }

You can replace with this:

    protected Location(int row, int column) {
    try {
        if (row > maxRows || column > maxColumns) {
            throw new InvalidLocationException("Invalid Location");
        }
        this.row = row;
        this.column = column;

}catch (InvalidLocationException ile) { System.out.println("I am from the try - catch block"); } }

Not tested, but it should give you a hint.

if this answers your question, please mark the question as answered.

Thanks