Skip to content

Instantly share code, notes, and snippets.

@agarasul
Last active February 19, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarasul/541eeb62c9ff391990b8641982d3a3e6 to your computer and use it in GitHub Desktop.
Save agarasul/541eeb62c9ff391990b8641982d3a3e6 to your computer and use it in GitHub Desktop.
public class Incrementor {
/**
* The current number
*/
private int number = 0;
/**
* The maximum value that number can be
*/
private int max = Integer.MAX_VALUE;
/**
* The method that increments current number
*/
public void incrementNumber() {
number++;
if (number == max) {
number = 0;
}
}
/**
* @return Current Maximum for number
*/
public int getMax() {
return max;
}
/**
* <p>The method that set the maximum value for number
*
* @param max the value that will set as maximum value
*/
public void setMax(int max) {
try {
if (max >= 0) {
this.max = max;
if (number > max) {
number = 0;
}
} else {
throw new Exception("Maximum value cannot be lower than number");
}
} catch (Exception e){}
}
/**
* @return Current number
*/
public int getNumber() {
return number;
}
}
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Unit Test for Incrementor
*/
public class ExampleUnitTest {
@Test
public void increment() {
Incrementor incrementor = new Incrementor();
incrementor.incrementNumber();
incrementor.incrementNumber();
incrementor.incrementNumber();
assertEquals(3, incrementor.getNumber());
}
@Test
public void setMaximum() {
Incrementor incrementor = new Incrementor();
try {
incrementor.setMax(10);
} catch (Exception e) {
assertEquals("Maximum value cannot be lower than number", e.getMessage());
}
assertEquals(10, incrementor.getMax());
}
@Test
public void setNegativeMaximum() {
Incrementor incrementor = new Incrementor();
try {
incrementor.setMax(-10);
Assert.fail("Expected exception");
} catch (Exception e) {
assertEquals("Maximum value cannot be lower than number", e.getMessage());
}
}
@Test
public void incrementNumberWithMaximum() {
Incrementor incrementor = new Incrementor();
try {
incrementor.setMax(10);
for (int i = 0; i < 10; i++) {
incrementor.incrementNumber();
}
assertEquals(0, incrementor.getNumber());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment