Created
May 20, 2015 04:04
-
-
Save Korilakkuma/295609a6f3978ce3faac to your computer and use it in GitHub Desktop.
JUint Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package junit.tutorial; | |
public class Calculator { | |
public int multiply(int x, int y) { | |
return x * y; | |
} | |
public float divide(int x, int y) { | |
if (y == 0) { | |
throw new IllegalArgumentException("divide by zero."); | |
} | |
return (float)x / (float)y; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package junitt.tutorial; | |
import static org.junit.Assert.*; | |
import static org.hamcrest.CoreMatchers.*; | |
import junit.tutorial.Calculator; | |
import org.junit.Test; | |
import org.junit.Before; | |
public class CalculatorTest { | |
private Calculator calculator = null; | |
@Before | |
public void setUp() throws Exception { | |
// Set up | |
this.calculator = new Calculator(); | |
} | |
@Test | |
public void multiplyTest3x4() { | |
int expected = 12; | |
// Exercise | |
int actual = this.calculator.multiply(3, 4); | |
// Verify | |
assertThat(actual, is(expected)); | |
} | |
@Test | |
public void multiplyTest5x7() { | |
int expected = 35; | |
// Exercise | |
int actual = this.calculator.multiply(5, 7); | |
// Verify | |
assertThat(actual, is(expected)); | |
} | |
@Test | |
public void divideTest3div2() { | |
float expected = 1.5f; | |
// Exercise | |
float actual = calculator.divide(3, 2); | |
// Verify | |
assertThat(actual, is(expected)); | |
} | |
@Test(expected = IllegalArgumentException.class) | |
public void divideTest5div0() { | |
// Exercise, Verify | |
this.calculator.divide(5, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment