Best Testng code snippet using org.testng.TestException
org.testng.TestException
It happens when testng is unable to run test method due to any reason
Here are code snippets that can help you understand more how developers are using
Source:TimestampIndexTests.java
...70 conn.connect();71 72 TGGraphObjectFactory gof = conn.getGraphObjectFactory();73 if (gof == null) {74 throw new org.testng.TestException("TG object factory is null");75 }76 77 TGGraphMetadata gmd = conn.getGraphMetadata(true);78 TGNodeType nodeTimestampIdxType = gmd.getNodeType("nodeTimestampIdx");79 if (nodeTimestampIdxType == null)80 throw new Exception("Node type not found");81 82 //Object[][] data = this.getTimestampData();83 List<TGNode> nodes = new ArrayList<TGNode>();84 for (int i=0; i<data.length; i++) {85 //System.out.println("CREATE ATTR:" + data[i][0]);86 TGNode node = gof.createNode(nodeTimestampIdxType);87 node.setAttribute("timestampAttr", data[i][0]);88 node.setAttribute("key", i);89 nodes.add(node);90 conn.insertEntity(node);91 }92 conn.commit();93 conn.disconnect();94 }95 96 /**97 * testReadTimestampData - Retrieve nodes and edge with timestamp index98 * @throws Exception99 */100 101 @Test(description = "Retrieve nodes and edge with timestamp index",102 dependsOnMethods = { "testCreateTimestampData" })103 public void testReadTimestampData() throws Exception {104 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);105 106 conn.connect();107 108 TGGraphObjectFactory gof = conn.getGraphObjectFactory();109 if (gof == null) {110 throw new org.testng.TestException("TG object factory is null");111 }112 113 conn.getGraphMetadata(true);114 TGKey tgKey = gof.createCompositeKey("nodeTimestampIdx");115 116 //Object[][] data = this.getTimestampData();117 for (int i=0; i<data.length; i++) {118 tgKey.setAttribute("key", i);119 TGEntity entity = conn.getEntity(tgKey, null);120 if (entity == null) {121 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");122 }123 //System.out.println("READ ATTR:" + entity.getAttribute("timestampAttr").getValue());124 // Assert on Node attribute125 Assert.assertEquals(entity.getAttribute("timestampAttr").getValue(), data[i][0]);126 }127 conn.disconnect();128 }129 130 /**131 * testUpdateTimestampData - Update timestamp index132 * @throws Exception133 */134 135 @Test(description = "Update timestamp index",136 dependsOnMethods = { "testReadTimestampData" },137 enabled = false)138 public void testUpdateTimestampData() throws Exception {139 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);140 141 conn.connect();142 143 TGGraphObjectFactory gof = conn.getGraphObjectFactory();144 if (gof == null) {145 throw new org.testng.TestException("TG object factory is null");146 }147 148 conn.getGraphMetadata(true);149 TGKey tgKey = gof.createCompositeKey("nodeTimestampIdx");150 151 //Object[][] data = this.getTimestampData();152 for (int i=0; i<data.length; i++) {153 tgKey.setAttribute("key", i);154 TGEntity entity = conn.getEntity(tgKey, null);155 if (entity == null) {156 throw new org.testng.TestException("TG entity #" + i + " was not retrieved");157 }158 //System.out.println("UPDATE ATTR:" + data[i][1] + " - Length:" + ((Timestamp) data[i][1]).length());159 entity.setAttribute("timestampAttr", data[i][1]); 160 conn.updateEntity(entity);161 conn.commit();162 }163 conn.disconnect();164 }165 166 /**167 * testReadUpdatedTimestampData - Retrieve nodes with updated timestamp index168 * @throws Exception169 */170 171 @Test(description = "Retrieve nodes with updated timestamp index",172 dependsOnMethods = { "testUpdateTimestampData" },173 enabled = false)174 public void testReadUpdatedTimestampData() throws Exception {175 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);176 177 conn.connect();178 179 TGGraphObjectFactory gof = conn.getGraphObjectFactory();180 if (gof == null) {181 throw new org.testng.TestException("TG object factory is null");182 }183 184 conn.getGraphMetadata(true);185 TGKey tgKey = gof.createCompositeKey("nodeTimestampIdx");186 187 //Object[][] data = this.getTimestampData();188 for (int i=0; i<data.length; i++) {189 tgKey.setAttribute("key", i);190 TGEntity entity = conn.getEntity(tgKey, null); 191 192 //System.out.println("READ UPDATED ATTR:" + entity.getAttribute("timestampAttr").getValue());193 // Assert on Node attribute194 // Assert.assertFalse(entity.getAttribute("timestampAttr").isNull(), "Expected attribute #"+i+" non null but found it null -");195 Assert.assertEquals(entity.getAttribute("timestampAttr").getValue(), data[i][1]);196 }197 conn.disconnect();198 }199 200 /**201 * testDeleteTimestampData - Delete timestamp index202 * @throws Exception203 */204 205 @Test(description = "Delete timestamp index",206 dependsOnMethods = { "testReadUpdatedTimestampData" },207 enabled = false)208 public void testDeleteTimestampData() throws Exception {209TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);210 211 conn.connect();212 213 TGGraphObjectFactory gof = conn.getGraphObjectFactory();214 if (gof == null) {215 throw new org.testng.TestException("TG object factory is null");216 }217 218 conn.getGraphMetadata(true);219 TGKey tgKey = gof.createCompositeKey("nodeTimestampIdx");220 221 //Object[][] data = this.getTimestampData();222 for (int i=0; i<data.length; i++) {223 tgKey.setAttribute("key", i);224 TGEntity entity = conn.getEntity(tgKey, null);225 if (entity == null) {226 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");227 }228 entity.setAttribute("timestampAttr", null); // delete the timestamp value by setting it up to null229 conn.updateEntity(entity);230 conn.commit();231 }232 conn.disconnect();233 }234 235 /**236 * testReadDeletedTimestampData - Retrieve nodes with deleted timestamp index237 * @throws Exception238 */239 240 @Test(description = "Retrieve nodes with deleted timestamp index",241 dependsOnMethods = { "testDeleteTimestampData" },242 enabled = false)243 public void testReadDeletedTimestampData() throws Exception {244 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);245 246 conn.connect();247 248 TGGraphObjectFactory gof = conn.getGraphObjectFactory();249 if (gof == null) {250 throw new org.testng.TestException("TG object factory is null");251 }252 253 conn.getGraphMetadata(true);254 TGKey tgKey = gof.createCompositeKey("nodeTimestampIdx");255 256 //Object[][] data = this.getTimestampData();257 for (int i=0; i<data.length; i++) {258 tgKey.setAttribute("key", i);259 TGEntity entity = conn.getEntity(tgKey, null); 260 261 // Assert on Node attribute262 Assert.assertTrue(entity.getAttribute("timestampAttr").isNull(), "Expected attribute #"+i+" null but found it non null -");263 }264 conn.disconnect();...
Source:AbstractTestNGSpringContextTests.java
1/*2 * Copyright 2002-2018 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * https://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.springframework.test.context.testng;17import java.lang.reflect.InvocationTargetException;18import java.lang.reflect.Method;19import org.apache.commons.logging.Log;20import org.apache.commons.logging.LogFactory;21import org.testng.IHookCallBack;22import org.testng.IHookable;23import org.testng.ITestResult;24import org.testng.annotations.AfterClass;25import org.testng.annotations.AfterMethod;26import org.testng.annotations.BeforeClass;27import org.testng.annotations.BeforeMethod;28import org.springframework.context.ApplicationContext;29import org.springframework.context.ApplicationContextAware;30import org.springframework.lang.Nullable;31import org.springframework.test.context.ContextConfiguration;32import org.springframework.test.context.TestContext;33import org.springframework.test.context.TestContextManager;34import org.springframework.test.context.TestExecutionListeners;35import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;36import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;37import org.springframework.test.context.support.DirtiesContextTestExecutionListener;38import org.springframework.test.context.web.ServletTestExecutionListener;39/**40 * Abstract base test class which integrates the <em>Spring TestContext Framework</em>41 * with explicit {@link ApplicationContext} testing support in a <strong>TestNG</strong>42 * environment.43 *44 * <p>Concrete subclasses:45 * <ul>46 * <li>Typically declare a class-level {@link ContextConfiguration47 * @ContextConfiguration} annotation to configure the {@linkplain ApplicationContext48 * application context} {@linkplain ContextConfiguration#locations() resource locations}49 * or {@linkplain ContextConfiguration#classes() annotated classes}. <em>If your test50 * does not need to load an application context, you may choose to omit the51 * {@code @ContextConfiguration} declaration and to configure the appropriate52 * {@link org.springframework.test.context.TestExecutionListener TestExecutionListeners}53 * manually.</em></li>54 * <li>Must have constructors which either implicitly or explicitly delegate to55 * {@code super();}.</li>56 * </ul>57 *58 * <p>The following {@link org.springframework.test.context.TestExecutionListener59 * TestExecutionListeners} are configured by default:60 *61 * <ul>62 * <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}63 * <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}64 * <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}65 * <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}66 * </ul>67 *68 * @author Sam Brannen69 * @author Juergen Hoeller70 * @since 2.571 * @see ContextConfiguration72 * @see TestContext73 * @see TestContextManager74 * @see TestExecutionListeners75 * @see ServletTestExecutionListener76 * @see DirtiesContextBeforeModesTestExecutionListener77 * @see DependencyInjectionTestExecutionListener78 * @see DirtiesContextTestExecutionListener79 * @see AbstractTransactionalTestNGSpringContextTests80 * @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests81 */82@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,83 DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })84public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {85 /** Logger available to subclasses. */86 protected final Log logger = LogFactory.getLog(getClass());87 /**88 * The {@link ApplicationContext} that was injected into this test instance89 * via {@link #setApplicationContext(ApplicationContext)}.90 */91 @Nullable92 protected ApplicationContext applicationContext;93 private final TestContextManager testContextManager;94 @Nullable95 private Throwable testException;96 /**97 * Construct a new AbstractTestNGSpringContextTests instance and initialize98 * the internal {@link TestContextManager} for the current test class.99 */100 public AbstractTestNGSpringContextTests() {101 this.testContextManager = new TestContextManager(getClass());102 }103 /**104 * Set the {@link ApplicationContext} to be used by this test instance,105 * provided via {@link ApplicationContextAware} semantics.106 * @param applicationContext the ApplicationContext that this test runs in107 */108 @Override109 public final void setApplicationContext(ApplicationContext applicationContext) {110 this.applicationContext = applicationContext;111 }112 /**113 * Delegates to the configured {@link TestContextManager} to call114 * {@linkplain TestContextManager#beforeTestClass() 'before test class'} callbacks.115 * @throws Exception if a registered TestExecutionListener throws an exception116 */117 @BeforeClass(alwaysRun = true)118 protected void springTestContextBeforeTestClass() throws Exception {119 this.testContextManager.beforeTestClass();120 }121 /**122 * Delegates to the configured {@link TestContextManager} to123 * {@linkplain TestContextManager#prepareTestInstance(Object) prepare} this test124 * instance prior to execution of any individual tests, for example for125 * injecting dependencies, etc.126 * @throws Exception if a registered TestExecutionListener throws an exception127 */128 @BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")129 protected void springTestContextPrepareTestInstance() throws Exception {130 this.testContextManager.prepareTestInstance(this);131 }132 /**133 * Delegates to the configured {@link TestContextManager} to134 * {@linkplain TestContextManager#beforeTestMethod(Object,Method) pre-process}135 * the test method before the actual test is executed.136 * @param testMethod the test method which is about to be executed137 * @throws Exception allows all exceptions to propagate138 */139 @BeforeMethod(alwaysRun = true)140 protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception {141 this.testContextManager.beforeTestMethod(this, testMethod);142 }143 /**144 * Delegates to the {@linkplain IHookCallBack#runTestMethod(ITestResult) test145 * method} in the supplied {@code callback} to execute the actual test146 * and then tracks the exception thrown during test execution, if any.147 * @see org.testng.IHookable#run(IHookCallBack, ITestResult)148 */149 @Override150 public void run(IHookCallBack callBack, ITestResult testResult) {151 Method testMethod = testResult.getMethod().getConstructorOrMethod().getMethod();152 boolean beforeCallbacksExecuted = false;153 try {154 this.testContextManager.beforeTestExecution(this, testMethod);155 beforeCallbacksExecuted = true;156 }157 catch (Throwable ex) {158 this.testException = ex;159 }160 if (beforeCallbacksExecuted) {161 callBack.runTestMethod(testResult);162 this.testException = getTestResultException(testResult);163 }164 try {165 this.testContextManager.afterTestExecution(this, testMethod, this.testException);166 }167 catch (Throwable ex) {168 if (this.testException == null) {169 this.testException = ex;170 }171 }172 if (this.testException != null) {173 throwAsUncheckedException(this.testException);174 }175 }176 /**177 * Delegates to the configured {@link TestContextManager} to178 * {@linkplain TestContextManager#afterTestMethod(Object, Method, Throwable)179 * post-process} the test method after the actual test has executed.180 *181 * @param testMethod the test method which has just been executed on the182 * test instance183 * @throws Exception allows all exceptions to propagate184 */185 @AfterMethod(alwaysRun = true)186 protected void springTestContextAfterTestMethod(Method testMethod) throws Exception {187 try {188 this.testContextManager.afterTestMethod(this, testMethod, this.testException);189 }190 finally {191 this.testException = null;192 }193 }194 /**195 * Delegates to the configured {@link TestContextManager} to call196 * {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks.197 * @throws Exception if a registered TestExecutionListener throws an exception198 */199 @AfterClass(alwaysRun = true)200 protected void springTestContextAfterTestClass() throws Exception {201 this.testContextManager.afterTestClass();202 }203 private Throwable getTestResultException(ITestResult testResult) {204 Throwable testResultException = testResult.getThrowable();205 if (testResultException instanceof InvocationTargetException) {206 testResultException = ((InvocationTargetException) testResultException).getCause();207 }208 return testResultException;209 }210 private RuntimeException throwAsUncheckedException(Throwable t) {211 throwAs(t);212 // Appeasing the compiler: the following line will never be executed.213 throw new IllegalStateException(t);214 }215 @SuppressWarnings("unchecked")216 private <T extends Throwable> void throwAs(Throwable t) throws T {217 throw (T) t;218 }219}...
Source:BooleanIndexTests.java
...55 conn.connect();56 57 TGGraphObjectFactory gof = conn.getGraphObjectFactory();58 if (gof == null) {59 throw new org.testng.TestException("TG object factory is null");60 }61 62 TGGraphMetadata gmd = conn.getGraphMetadata(true);63 TGNodeType nodetype = gmd.getNodeType("boolNodetype");64 if (nodetype == null)65 throw new Exception("Node type not found");66 67 Object[][] data = this.getBooleanData();68 for (int i=0; i<data.length; i++) {69 TGNode node = gof.createNode(nodetype);70 node.setAttribute("key", i);71 node.setAttribute("boolAttr", data[i][0]);72 73 conn.insertEntity(node);74 //System.out.println("data " + data[i][0]);75 }76 77 conn.commit();78 conn.disconnect();79 }80 81 /**82 * testReadBooleanIndex - Retrieve nodes by boolean index83 * @throws Exception84 */85 86 @Test(description = "Retrieve nodes by boolean index",87 dependsOnMethods = { "testCreateBooleanIndex" })88 public void testReadBooleanIndex() throws Exception {89 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);90 91 conn.connect();92 93 TGGraphObjectFactory gof = conn.getGraphObjectFactory();94 if (gof == null) {95 throw new org.testng.TestException("TG object factory is null");96 }97 98 conn.getGraphMetadata(true);99 TGKey tgKey = gof.createCompositeKey("boolNodetype");100 101 Object[][] data = this.getBooleanData();102 for (int i=0; i<data.length; i++) {103 tgKey.setAttribute("boolAttr", data[i][0]);104 TGEntity entity = conn.getEntity(tgKey, null);105 if (entity == null) {106 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");107 }108 // System.out.println("READ ATTR :" + data[i][0]);109 // Assert on Node attribute110 Assert.assertEquals(entity.getAttribute("key").getValue(), i);111 }112 conn.disconnect();113 }114 115 /**116 * testUpdateBooleanData - Update boolean index117 * @throws Exception118 */119 120 @Test(description = "Update boolean index",121 dependsOnMethods = { "testReadBooleanIndex" },122 enabled = false)123 public void testUpdateBooleanIndex() throws Exception {124 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);125 126 conn.connect();127 128 TGGraphObjectFactory gof = conn.getGraphObjectFactory();129 if (gof == null) {130 throw new org.testng.TestException("TG object factory is null");131 }132 133 conn.getGraphMetadata(true);134 TGKey tgKey = gof.createCompositeKey("boolNodetype");135 136 Object[][] data = this.getBooleanData();137 for (int i=0; i<data.length; i++) {138 tgKey.setAttribute("key", i);139 TGEntity entity = conn.getEntity(tgKey, null);140 if (entity == null) {141 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");142 }143 entity.setAttribute("boolAttr", data[i][1]); 144 conn.updateEntity(entity);145 conn.commit();146 }147 conn.disconnect();148 }149 150 /**151 * testReadUpdatedBooleanIndex - Retrieve nodes with updated boolean index152 * @throws Exception153 */154 155 @Test(description = "Retrieve nodes with updated boolean index",156 dependsOnMethods = { "testUpdateBooleanIndex" },157 enabled = false)158 public void testReadUpdatedBooleanIndex() throws Exception {159 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);160 161 conn.connect();162 163 TGGraphObjectFactory gof = conn.getGraphObjectFactory();164 if (gof == null) {165 throw new org.testng.TestException("TG object factory is null");166 }167 168 conn.getGraphMetadata(true);169 TGKey tgKey = gof.createCompositeKey("boolNodetype");170 171 Object[][] data = this.getBooleanData();172 for (int i=0; i<data.length; i++) {173 tgKey.setAttribute("boolAttr", data[i][1]);174 TGEntity entity = conn.getEntity(tgKey, null); 175 if (entity == null) {176 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");177 }178 179 // Assert on Node attribute180 //Assert.assertFalse(entity.getAttribute("boolAttr").isNull(), "Expected attribute #"+i+" non null but found it null -");181 Assert.assertEquals(entity.getAttribute("key").getValue(), i);182 }183 conn.disconnect();184 }185 186 /**187 * testDeleteBooleanIndex - Delete boolean index188 * @throws Exception189 */190 191 @Test(description = "Delete boolean index",192 dependsOnMethods = { "testReadUpdatedBooleanIndex" },193 enabled = false)194 public void testDeleteBooleanIndex() throws Exception {195 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);196 197 conn.connect();198 199 TGGraphObjectFactory gof = conn.getGraphObjectFactory();200 if (gof == null) {201 throw new org.testng.TestException("TG object factory is null");202 }203 204 conn.getGraphMetadata(true);205 TGKey tgKey = gof.createCompositeKey("boolNodetype");206 207 Object[][] data = this.getBooleanData();208 for (int i=0; i<data.length; i++) {209 tgKey.setAttribute("key", i);210 TGEntity entity = conn.getEntity(tgKey, null);211 if (entity == null) {212 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");213 }214 entity.setAttribute("boolAttr", null); // nullify the boolean value. Null should fail for index215 conn.updateEntity(entity);216 conn.commit();217 }218 conn.disconnect();219 }220 221 /**222 * testReadDeletedBooleanIndex - Retrieve nodes with updated boolean index223 * @throws Exception224 */225 @Test(description = "Retrieve nodes with deleted boolean attribute",226 dependsOnMethods = { "testDeleteBooleanIndex" },227 enabled = false) 228 public void testReadDeletedBooleanIndex() throws Exception {229 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);230 231 conn.connect();232 233 TGGraphObjectFactory gof = conn.getGraphObjectFactory();234 if (gof == null) {235 throw new org.testng.TestException("TG object factory is null");236 }237 238 conn.getGraphMetadata(true);239 TGKey tgKey = gof.createCompositeKey("boolNodetype");240 241 Object[][] data = this.getBooleanData();242 for (int i=0; i<data.length; i++) {243 tgKey.setAttribute("boolAttr", null);244 TGEntity entity = conn.getEntity(tgKey, null); 245 if (entity == null) {246 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");247 }248 249 // Assert on Node attribute250 Assert.assertTrue(entity.getAttribute("boolAttr").isNull(), "Expected attribute #"+i+" null but found it non null -");251 }252 conn.disconnect();253 }254 255 /************************256 * 257 * Data Providers 258 * 259 ************************/260 ...
Source:StringIndexTests.java
...52 conn.connect();53 54 TGGraphObjectFactory gof = conn.getGraphObjectFactory();55 if (gof == null) {56 throw new org.testng.TestException("TG object factory is null");57 }58 59 TGGraphMetadata gmd = conn.getGraphMetadata(true);60 TGNodeType nodeStringIdxType = gmd.getNodeType("nodeStringIdx");61 if (nodeStringIdxType == null)62 throw new Exception("Node type not found");63 64 Object[][] data = this.getStringData();65 for (int i=0; i<data.length; i++) {66 TGNode node = gof.createNode(nodeStringIdxType);67 node.setAttribute("stringAttr", data[i][0]);68 node.setAttribute("key", i);69 conn.insertEntity(node);70 System.out.println("Key : " + i + " strIdx : " + data[i][0]);71 }72 conn.commit();73 conn.disconnect();74 }75 76 /**77 * testReadStringIndex - Retrieve nodes by string index78 * @throws Exception79 */80 81 @Test(description = "Retrieve nodes by string index",82 dependsOnMethods = { "testCreateStringIndex" })83 public void testReadStringIndex() throws Exception {84 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);85 86 conn.connect();87 88 TGGraphObjectFactory gof = conn.getGraphObjectFactory();89 if (gof == null) {90 throw new org.testng.TestException("TG object factory is null");91 }92 93 conn.getGraphMetadata(true);94 TGKey tgKey = gof.createCompositeKey("nodeStringIdx");95 96 Object[][] data = this.getStringData();97 for (int i=0; i<data.length; i++) {98 tgKey.setAttribute("stringAttr", data[i][0]);99 TGEntity entity = conn.getEntity(tgKey, null);100 if (entity == null) {101 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");102 }103 // System.out.println("READ ATTR :" + data[i][0]);104 // Assert on Node attribute105 Assert.assertEquals(entity.getAttribute("key").getValue(), i);106 }107 conn.disconnect();108 }109 110 /**111 * testUpdateStringIndex - Update string index112 * @throws Exception113 */114 115 @Test(description = "Update string index",116 dependsOnMethods = { "testCreateStringIndex" },117 enabled = false)118 public void testUpdateStringIndex() throws Exception {119TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);120 121 conn.connect();122 123 TGGraphObjectFactory gof = conn.getGraphObjectFactory();124 if (gof == null) {125 throw new org.testng.TestException("TG object factory is null");126 }127 128 conn.getGraphMetadata(true);129 TGKey tgKey = gof.createCompositeKey("nodeStringIdx");130 131 Object[][] data = this.getStringData();132 for (int i=0; i<data.length; i++) {133 tgKey.setAttribute("key", i);134 TGEntity entity = conn.getEntity(tgKey, null);135 if (entity == null) {136 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");137 }138 entity.setAttribute("stringAttr", data[i][1]); 139 conn.updateEntity(entity);140 conn.commit();141 }142 conn.disconnect();143 }144 145 /**146 * testReadUpdatedStringIndex - Retrieve nodes with updated string index147 * @throws Exception148 */149 150 @Test(description = "Retrieve nodes with updated string index",151 dependsOnMethods = { "testUpdateStringIndex" },152 enabled = false)153 public void testReadUpdatedStringIndex() throws Exception {154 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);155 156 conn.connect();157 158 TGGraphObjectFactory gof = conn.getGraphObjectFactory();159 if (gof == null) {160 throw new org.testng.TestException("TG object factory is null");161 }162 163 conn.getGraphMetadata(true);164 TGKey tgKey = gof.createCompositeKey("nodeStringIdx");165 166 Object[][] data = this.getStringData();167 for (int i=0; i<data.length; i++) {168 tgKey.setAttribute("stringAttr", data[i][1]);169 TGEntity entity = conn.getEntity(tgKey, null); 170 if (entity == null) {171 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");172 }173 174 // Assert on Node attribute175 // Assert.assertFalse(entity.getAttribute("stringAttr").isNull(), "Expected attribute #"+i+" non null but found it null -");176 Assert.assertEquals(entity.getAttribute("key").getValue(), i);177 }178 conn.disconnect();179 }180 181 /**182 * testDeleteStringIndex - Delete string index183 * @throws Exception184 */185 186 @Test(description = "Delete string index",187 dependsOnMethods = { "testReadUpdatedStringIndex" },188 enabled = false)189 public void testDeleteStringIndex() throws Exception {190TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);191 192 conn.connect();193 194 TGGraphObjectFactory gof = conn.getGraphObjectFactory();195 if (gof == null) {196 throw new org.testng.TestException("TG object factory is null");197 }198 199 conn.getGraphMetadata(true);200 TGKey tgKey = gof.createCompositeKey("nodeStringIdx");201 202 Object[][] data = this.getStringData();203 for (int i=0; i<data.length; i++) {204 tgKey.setAttribute("key", 0);205 TGEntity entity = conn.getEntity(tgKey, null);206 if (entity == null) {207 throw new org.testng.TestException("TG entity #"+i+" was not retrieved");208 }209 entity.setAttribute("stringAttr", null); // delete the boolean value210 conn.updateEntity(entity);211 conn.commit();212 }213 conn.disconnect();214 }215 216 /**217 * testReadDeletedStringIndex - Retrieve nodes with deleted string index218 * @throws Exception219 */220 221 @Test(description = "Retrieve nodes with deleted string index",222 dependsOnMethods = { "testDeleteStringIndex" },223 enabled = false)224 public void testReadDeletedStringIndex() throws Exception {225 TGConnection conn = TGConnectionFactory.getInstance().createConnection(tgUrl, tgUser, tgPwd, null);226 227 conn.connect();228 229 TGGraphObjectFactory gof = conn.getGraphObjectFactory();230 if (gof == null) {231 throw new org.testng.TestException("TG object factory is null");232 }233 234 conn.getGraphMetadata(true);235 TGKey tgKey = gof.createCompositeKey("nodeStringIdx");236 237 Object[][] data = this.getStringData();238 for (int i=0; i<data.length; i++) {239 tgKey.setAttribute("key", 0);240 TGEntity entity = conn.getEntity(tgKey, null); 241 242 // Assert on Node attribute243 Assert.assertTrue(entity.getAttribute("stringAttr").isNull(), "Expected attribute #"+i+" null but found it non null -");244 }245 conn.disconnect();...
Source:SOAPTest.java
1package com.qaprosoft.carina.demo.base;2import com.qaprosoft.carina.core.foundation.AbstractTest;3import org.springframework.context.ApplicationContext;4import org.springframework.context.ApplicationContextAware;5import org.springframework.lang.Nullable;6import org.springframework.test.context.TestContextManager;7import org.springframework.test.context.TestExecutionListeners;8import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;9import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;10import org.springframework.test.context.support.DirtiesContextTestExecutionListener;11import org.springframework.test.context.web.ServletTestExecutionListener;12import org.testng.IHookCallBack;13import org.testng.IHookable;14import org.testng.ITestResult;15import org.testng.annotations.AfterClass;16import org.testng.annotations.BeforeClass;17import org.testng.annotations.BeforeMethod;18import java.lang.reflect.InvocationTargetException;19import java.lang.reflect.Method;20@TestExecutionListeners({ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,21 DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})22public class SOAPTest extends AbstractTest implements IHookable, ApplicationContextAware {23 /**24 * The {@link ApplicationContext} that was injected into this test instance25 * via {@link #setApplicationContext(ApplicationContext)}.26 */27 @Nullable28 protected ApplicationContext applicationContext;29 private final TestContextManager testContextManager;30 @Nullable31 private Throwable testException;32 /**33 * Construct a new SoapTest instance and initialize34 * the internal {@link TestContextManager} for the current test class.35 */36 public SOAPTest() {37 this.testContextManager = new TestContextManager(getClass());38 }39 /**40 * Set the {@link ApplicationContext} to be used by this test instance,41 * provided via {@link ApplicationContextAware} semantics.42 *43 * @param applicationContext the ApplicationContext that this test runs in44 */45 @Override46 public final void setApplicationContext(ApplicationContext applicationContext) {47 this.applicationContext = applicationContext;48 }49 /**50 * Delegates to the configured {@link TestContextManager} to call51 * {@linkplain TestContextManager#beforeTestClass() 'before test class'} callbacks.52 *53 * @throws Exception if a registered TestExecutionListener throws an exception54 */55 @BeforeClass(alwaysRun = true)56 protected void springTestContextBeforeTestClass() throws Exception {57 this.testContextManager.beforeTestClass();58 }59 /**60 * Delegates to the configured {@link TestContextManager} to61 * {@linkplain TestContextManager#prepareTestInstance(Object) prepare} this test62 * instance prior to execution of any individual tests, for example for63 * injecting dependencies, etc.64 *65 * @throws Exception if a registered TestExecutionListener throws an exception66 */67 @BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")68 protected void springTestContextPrepareTestInstance() throws Exception {69 this.testContextManager.prepareTestInstance(this);70 }71 /**72 * Delegates to the configured {@link TestContextManager} to73 * {@linkplain TestContextManager#beforeTestMethod(Object, Method) pre-process}74 * the test method before the actual test is executed.75 *76 * @param testMethod the test method which is about to be executed77 * @throws Exception allows all exceptions to propagate78 */79 @BeforeMethod(alwaysRun = true)80 protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception {81 this.testContextManager.beforeTestMethod(this, testMethod);82 }83 /**84 * Delegates to the {@linkplain IHookCallBack#runTestMethod(ITestResult) test85 * method} in the supplied {@code callback} to execute the actual test86 * and then tracks the exception thrown during test execution, if any.87 *88 * @see org.testng.IHookable#run(IHookCallBack, ITestResult)89 */90 @Override91 public void run(IHookCallBack callBack, ITestResult testResult) {92 Method testMethod = testResult.getMethod().getConstructorOrMethod().getMethod();93 boolean beforeCallbacksExecuted = false;94 try {95 this.testContextManager.beforeTestExecution(this, testMethod);96 beforeCallbacksExecuted = true;97 } catch (Throwable ex) {98 this.testException = ex;99 }100 if (beforeCallbacksExecuted) {101 callBack.runTestMethod(testResult);102 this.testException = getTestResultException(testResult);103 }104 try {105 this.testContextManager.afterTestExecution(this, testMethod, this.testException);106 } catch (Throwable ex) {107 if (this.testException == null) {108 this.testException = ex;109 }110 }111 if (this.testException != null) {112 throwAsUncheckedException(this.testException);113 }114 }115 /**116 * Delegates to the configured {@link TestContextManager} to call117 * {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks.118 *119 * @throws Exception if a registered TestExecutionListener throws an exception120 */121 @AfterClass(alwaysRun = true)122 protected void springTestContextAfterTestClass() throws Exception {123 this.testContextManager.afterTestClass();124 }125 private Throwable getTestResultException(ITestResult testResult) {126 Throwable testResultException = testResult.getThrowable();127 if (testResultException instanceof InvocationTargetException) {128 testResultException = ((InvocationTargetException) testResultException).getCause();129 }130 return testResultException;131 }132 private RuntimeException throwAsUncheckedException(Throwable t) {133 throwAs(t);134 // Appeasing the compiler: the following line will never be executed.135 throw new IllegalStateException(t);136 }137 @SuppressWarnings("unchecked")138 private <T extends Throwable> void throwAs(Throwable t) throws T {139 throw (T) t;140 }141}...
Source:ExpectedExceptionsHolder.java
1package org.testng.internal;2import org.testng.IExpectedExceptionsHolder;3import org.testng.ITestNGMethod;4import org.testng.TestException;5import org.testng.annotations.ITestAnnotation;6import org.testng.internal.annotations.IAnnotationFinder;7import java.util.Arrays;8public class ExpectedExceptionsHolder {9 protected final IAnnotationFinder finder;10 protected final ITestNGMethod method;11 private final Class<?>[] expectedClasses;12 private final IExpectedExceptionsHolder holder;13 protected ExpectedExceptionsHolder(14 IAnnotationFinder finder, ITestNGMethod method, IExpectedExceptionsHolder holder) {15 this.finder = finder;16 this.method = method;17 expectedClasses = findExpectedClasses(finder, method);18 this.holder = holder;19 }20 private static Class<?>[] findExpectedClasses(IAnnotationFinder finder, ITestNGMethod method) {21 ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);22 if (testAnnotation != null) {23 return testAnnotation.getExpectedExceptions();24 }25 return new Class<?>[0];26 }27 /**28 * @param ite The exception that was just thrown29 * @return true if the exception that was just thrown is part of the expected exceptions30 */31 public boolean isExpectedException(Throwable ite) {32 if (!hasExpectedClasses()) {33 return false;34 }35 // TestException is the wrapper exception that TestNG will be throwing when an exception was36 // expected but not thrown37 if (ite.getClass() == TestException.class) {38 return false;39 }40 Class<?> realExceptionClass = ite.getClass();41 for (Class<?> exception : expectedClasses) {42 if (exception.isAssignableFrom(realExceptionClass) && holder.isThrowableMatching(ite)) {43 return true;44 }45 }46 return false;47 }48 public Throwable wrongException(Throwable ite) {49 if (!hasExpectedClasses()) {50 return ite;51 }52 if (holder.isThrowableMatching(ite)) {53 return new TestException(54 "Expected exception of " + getExpectedExceptionsPluralize() + " but got " + ite, ite);55 } else {56 return new TestException(holder.getWrongExceptionMessage(ite), ite);57 }58 }59 public TestException noException(ITestNGMethod testMethod) {60 if (!hasExpectedClasses()) {61 return null;62 }63 return new TestException(64 "Method "65 + testMethod66 + " should have thrown an exception of "67 + getExpectedExceptionsPluralize());68 }69 private boolean hasExpectedClasses() {70 return expectedClasses != null && expectedClasses.length > 0;71 }72 private String getExpectedExceptionsPluralize() {73 StringBuilder sb = new StringBuilder();74 if (expectedClasses.length > 1) {75 sb.append("any of types ");76 sb.append(Arrays.toString(expectedClasses));77 } else {...
TestException
Using AI Code Generation
1import org.testng.TestException;2import org.testng.ITestResult;3import org.testng.IRetryAnalyzer;4import org.testng.IAnnotationTransformer;5import org.testng.annotations.ITestAnnotation;6public class RetryAnalyzer implements IRetryAnalyzer, IAnnotationTransformer {7public boolean retry(ITestResult result) {8if (result.getStatus() == ITestResult.FAILURE) {9if (result.getThrowable() instanceof TestException) {10return true;11}12}13return false;14}15public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {16annotation.setRetryAnalyzer(RetryAnalyzer.class);17}18}19@RetryAnalyzer(RetryAnalyzer.class)20public void test1() {21System.out.println("test1");22Assert.assertTrue(false);23}24@RetryAnalyzer(RetryAnalyzer.class)25public void test2() {26System.out.println("test2");27Assert.assertTrue(true);28}29@RetryAnalyzer(RetryAnalyzer.class)30public void test3() {31System.out.println("test3");32Assert.assertTrue(false);33}
TestException
Using AI Code Generation
1import org.testng.TestException;2import org.testng.annotations.Test;3import org.testng.asserts.SoftAssert;4public class TestSoftAssert {5 public void testSoftAssert() {6 SoftAssert softAssert = new SoftAssert();7 softAssert.assertEquals(1, 1);8 softAssert.assertEquals(2, 2);9 softAssert.assertEquals(3, 3);10 softAssert.assertEquals(4, 4);11 softAssert.assertEquals(5, 5);12 softAssert.assertAll();13 }14 public void testSoftAssertWithException() {15 SoftAssert softAssert = new SoftAssert();16 softAssert.assertEquals(1, 1);17 softAssert.assertEquals(2, 2);18 softAssert.assertEquals(3, 3);19 softAssert.assertEquals(4, 4);20 softAssert.assertEquals(5, 5);21 throw new TestException("Test Exception");22 }23 public void testSoftAssertWithException2() {24 SoftAssert softAssert = new SoftAssert();25 softAssert.assertEquals(1, 1);26 softAssert.assertEquals(2, 2);27 softAssert.assertEquals(3, 3);28 softAssert.assertEquals(4, 4);29 softAssert.assertEquals(5, 5);30 softAssert.assertAll();31 throw new TestException("Test Exception");32 }33}34[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestNG ---35[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestNG ---
TestException
Using AI Code Generation
1import org.testng.TestException;2import org.testng.annotations.Test;3public class TestNGTest {4 public void test1() {5 System.out.println("Test 1");6 }7 public void test2() {8 System.out.println("Test 2");9 }10 @Test(expectedExceptions = TestException.class)11 public void test3() {12 System.out.println("Test 3");13 throw new TestException("Test Exception");14 }15 @Test(expectedExceptions = TestException.class)16 public void test4() {17 System.out.println("Test 4");18 }19}20 at TestNGTest.test4(TestNGTest.java:23)21 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)22 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)23 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)24 at java.lang.reflect.Method.invoke(Method.java:497)25 at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)26 at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)27 at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)28 at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)29 at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)30 at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)31 at org.testng.TestRunner.privateRun(TestRunner.java:767)32 at org.testng.TestRunner.run(TestRunner.java:617)33 at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)34 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)35 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)36 at org.testng.SuiteRunner.run(SuiteRunner.java:240)37 at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)38 at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)39 at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
TestException
Using AI Code Generation
1import org.testng.TestException;2import org.testng.annotations.Test;3public class TestNGTestException {4public void testException(){5throw new TestException("TestNG TestException");6}7}8at com.qtpselenium.testng.TestNGTestException.testException(TestNGTestException.java:15)9at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)10at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)11at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)12at java.lang.reflect.Method.invoke(Method.java:606)13at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)14at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)15at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)16at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)17at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)18at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)19at org.testng.TestRunner.privateRun(TestRunner.java:767)20at org.testng.TestRunner.run(TestRunner.java:617)21at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)22at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)23at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)24at org.testng.SuiteRunner.run(SuiteRunner.java:240)25at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)26at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)27at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)28at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)29at org.testng.TestNG.run(TestNG.java:1057)30at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)31at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)32at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)33at com.qtpselenium.testng.TestNGTestException.testException(TestNGTestException.java:15)34at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
TestException
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.Assert;3import org.testng.TestException;4public class TestNGException {5 public void testException() {6 try {7 throw new Exception("Test Exception");8 } catch (Exception e) {9 e.printStackTrace();10 }11 }12}
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!