assertNotSame() is a built in JUint function to test your java programs before deploying the application on your production environment/server. In this tutorial, we are going to share the junit assertNotSame example.
assertNotSame() function checks two objects do not refer to the same object. If they do refer to the same object, an AssertionError is thrown.
Example 1. assertNotSame() Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Test public void testApplicationReport() { long timestamp = System.currentTimeMillis(); ApplicationReport appReport1 = createApplicationReport(1, 1, timestamp); ApplicationReport appReport2 = createApplicationReport(1, 1, timestamp); ApplicationReport appReport3 = createApplicationReport(1, 1, timestamp); Assert.assertEquals(appReport1, appReport2); Assert.assertEquals(appReport2, appReport3); appReport1.setApplicationId(null); Assert.assertNull(appReport1.getApplicationId()); Assert.assertNotSame(appReport1, appReport2); appReport2.setCurrentApplicationAttemptId(null); Assert.assertNull(appReport2.getCurrentApplicationAttemptId()); Assert.assertNotSame(appReport2, appReport3); Assert.assertNull(appReport1.getAMRMToken()); } |
Example 2. assertNotSame() Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Test public void testDuplicateCreate() { Source avroSource1 = sourceFactory.create("avroSource1", "avro"); Source avroSource2 = sourceFactory.create("avroSource2", "avro"); Assert.assertNotNull(avroSource1); Assert.assertNotNull(avroSource2); Assert.assertNotSame(avroSource1, avroSource2); Assert.assertTrue(avroSource1 instanceof AvroSource); Assert.assertTrue(avroSource2 instanceof AvroSource); Source s1 = sourceFactory.create("avroSource1", "avro"); Source s2 = sourceFactory.create("avroSource2", "avro"); Assert.assertNotSame(avroSource1, s1); Assert.assertNotSame(avroSource2, s2); } |
Example 3. assertNotSame() Function.
1 2 3 4 5 6 7 8 9 10 |
@Test public void testValueOf() { Value theEnum = Value.valueOf("THREE"); Assert.assertEquals("THREE", theEnum.name()); Assert.assertSame(theEnum, Value.THREE); Assert.assertSame(Value.THREE, Value.THREE); Value theValue = Value.TWO; Assert.assertNotSame(Value.THREE, theValue); Assert.assertSame(Value.TWO, theValue); } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.