I don't really like Softly Asserts, and I notice that a lot of developers at work seem to use it all the time.
And since it's Christmas today (Merry Christmas!), let's get into the spirit of the season by testing if there is a Miracle on 34th Street.
We are going to use SoftlyAssertions with the following test written below:
And it looks fine if the tests run properly.
But when the tests fail, likely as not we see NullPointerExceptions instead of useful asserts.
public class SoftlyAssertionTest { | |
@Test | |
public void testMiracleAt34thStreet() { | |
Town newYork = new Town("New York"); | |
Street thirtyfourthStreet = new Street("34th Street", newYork); | |
Address address = new Address(5, thirtyfourthStreet); | |
Miracle miracle = new Miracle(address); | |
SoftAssertions softly = new SoftAssertions(); | |
softly.assertThat(miracle).isNotNull(); | |
softly.assertThat(miracle.getAddress().getHousenumber()).isEqualTo(5); | |
softly.assertThat(miracle.getAddress().getStreet().getStreetname()).isEqualTo("34th Street"); | |
softly.assertThat(miracle.getAddress().getStreet().getTown().getTownname()).isEqualTo("New York"); | |
softly.assertAll(); | |
} | |
} |
Let's try to fail the test, by removing the town.
And lo and behold, we get this:
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:571)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
I really would like for SoftAssertions to only be used during checking of simple properties within an object, thanks!
And not this cross-boundary stuff.