Created
November 26, 2013 01:00
-
-
Save tomkel5/7651732 to your computer and use it in GitHub Desktop.
Testing Differences by Testing Similarities
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 com.tomkel; | |
import org.testng.Assert; | |
import org.testng.annotations.DataProvider; | |
import org.testng.annotations.Test; | |
@Test | |
public class ColorTest { | |
public enum Temperature { | |
WARM, | |
COLD; | |
} | |
public class WavelengthRange { | |
public float lowerBoundary; | |
public float upperBoundary; | |
public WavelengthRange(float lowerBoundary, float upperBoundary) { | |
this.lowerBoundary = lowerBoundary; | |
this.upperBoundary = upperBoundary; | |
} | |
} | |
private class Color { | |
public String name; | |
public int number; | |
public boolean primary; | |
public Temperature temperature; | |
public String[] variants; | |
public WavelengthRange wavelengthRange; | |
public Color(String name, int number, boolean primary, Temperature temperature, String[] variants, WavelengthRange wavelengthRange) { | |
this.name = name; | |
this.number = number; | |
this.primary = primary; | |
this.temperature = temperature; | |
this.variants = variants; | |
this.wavelengthRange = wavelengthRange; | |
} | |
} | |
@Test(dataProvider = "same") | |
public void testSame(Color actual, Color expected) { | |
// These are pretty normal | |
Assert.assertEquals(actual.name, expected.name); | |
Assert.assertEquals(actual.number, expected.number); | |
Assert.assertEquals(actual.primary, expected.primary); | |
Assert.assertEquals(actual.temperature, expected.temperature); | |
// A little bit more complex... | |
// Check if the lengths are the same: | |
Assert.assertEquals(actual.variants.length, expected.variants.length); | |
// Make sure everything in "expected" is reflected in "actual": | |
for (String expectedVariant : expected.variants) { | |
boolean found = false; | |
for (String actualVariant : actual.variants) { | |
if (actualVariant.equals(expectedVariant)) { | |
found = true; | |
} | |
} | |
Assert.assertTrue(found, String.format("Missing '%s' in actual variants", expectedVariant)); | |
} | |
// Then do the reverse: | |
for (String actualVariant : expected.variants) { | |
boolean found = false; | |
for (String expectedVariant : expected.variants) { | |
if (expectedVariant.equals(actualVariant)) { | |
found = true; | |
} | |
} | |
Assert.assertTrue(found, String.format("Missing '%s' in expected variants", actualVariant)); | |
} | |
// Yikes... Getting really complex... I think you get the point, so I won't even bother here... | |
// Assert.assertEquals(actual.wavelengthRange, expected.wavelengthRange); | |
} | |
@DataProvider(name = "same") | |
public Object[][] same() { | |
return new Object[][] { | |
{ | |
new Color("blue", 5, true, Temperature.COLD, new String[] {"aqua", "turquoise"}, new WavelengthRange(450f, 495f)), | |
new Color("blue", 5, true, Temperature.COLD, new String[] {"aqua", "turquoise"}, new WavelengthRange(450f, 495f)) | |
}, | |
{ | |
new Color("red", 1, true, Temperature.WARM, new String[] {"maroon", "crimson"}, new WavelengthRange(620f, 740f)), | |
new Color("red", 1, true, Temperature.WARM, new String[] {"maroon", "crimson"}, new WavelengthRange(620f, 740f)), | |
} | |
}; | |
} | |
@Test(dataProvider = "different", expectedExceptions = {AssertionError.class}) | |
public void testDifferent(Color actual, Color expected) throws Exception { | |
this.testSame(actual, expected); | |
} | |
@DataProvider(name = "different") | |
public Object[][] different() { | |
return new Object[][] { | |
{ | |
new Color("red", 1, true, Temperature.WARM, new String[] {"maroon", "crimson"}, new WavelengthRange(620f, 740f)), | |
new Color("blue", 5, true, Temperature.COLD, new String[] {"aqua", "turquoise"}, new WavelengthRange(450f, 495f)) | |
}, | |
{ | |
new Color("yellow", 3, true, Temperature.WARM, new String[] {"lemon"}, new WavelengthRange(570f, 590f)), | |
new Color("green", 4, false, Temperature.COLD, new String[] {"olive", "kelly"}, new WavelengthRange(520f, 570f)) | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment