TestNg support?

65 views
Skip to first unread message

Sonni Nielsen

unread,
May 9, 2012, 12:14:29 AM5/9/12
to smock...@googlegroups.com
Hi

I want to use Smock in combination with Smock, but I run in to the following problem:

My testCase is extending AbstractTestNGSpringContextTests in order to use the application-context, which makes it impossible to use AbstractSmockServerTest as well.

So I wanted to know if there is any other way I can use TestNg in combination with Smock? Or should i just bite the bullet and create a AbstractTestNGSpringContextSmockServerTest :)

Cheers

lu...@krecan.net

unread,
May 9, 2012, 1:55:14 PM5/9/12
to smock...@googlegroups.com, Sonni Nielsen
Hi,
that is the trouble with lack of multiple inheritance in Java. You have two options.
1. Replicate code in AbstractSmockServerTest in your test classes
2. Use Spring annotations to botstrap Spring into TestNG

I would prefer the option 2. I think that is the recommended way anyway. If you have further questions, do not hesitate to ask.
Cheers
Lukas

Sonni Nielsen <sonni....@gmail.com> napsal(a):

--
Odesláno z mého telefonu s Androidem pomocí pošty Kaiten Mail. Omluvte prosím mou stručnost.

Sonni Nielsen

unread,
Jun 4, 2012, 5:53:47 PM6/4/12
to lu...@krecan.net, smock...@googlegroups.com
FYI

I came up with the following solution:

@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
public class AbstractSpringContextEndpointTest extends AbstractSmockServerTest  {
  protected final Log logger = LogFactory.getLog(getClass());

  protected ApplicationContext applicationContext;

  private final TestContextManager testContextManager;

  private Throwable testException;

  public AbstractSpringContextEndpointTest() {
    this.testContextManager = new TestContextManager(getClass());
  }
  
//-----------------------------------------------------
// Methods taken from AbstractTestNGSpringContextTests
//-----------------------------------------------------
  
  public final void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
  }

  @BeforeClass(alwaysRun = true)
  protected void springTestContextBeforeTestClass() throws Exception {
    this.testContextManager.beforeTestClass();
  }

  @BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
  protected void springTestContextPrepareTestInstance() throws Exception {
    this.testContextManager.prepareTestInstance(this);
  }

  @BeforeMethod(alwaysRun = true)
  protected void springTestContextBeforeTestMethod(Method testMethod) throws Exception {
    this.testContextManager.beforeTestMethod(this, testMethod);
  }

  public void run(IHookCallBack callBack, ITestResult testResult) {
    callBack.runTestMethod(testResult);

    Throwable testResultException = testResult.getThrowable();
    if (testResultException instanceof InvocationTargetException) {
      testResultException = ((InvocationTargetException) testResultException).getCause();
    }
    this.testException = testResultException;
  }

  @AfterMethod(alwaysRun = true)
  protected void springTestContextAfterTestMethod(Method testMethod) throws Exception {
    try {
      this.testContextManager.afterTestMethod(this, testMethod, this.testException);
    }
    finally {
      this.testException = null;
    }
  }

  @AfterClass(alwaysRun = true)
  protected void springTestContextAfterTestClass() throws Exception {
    this.testContextManager.afterTestClass();
  }
}

This works fine and my tests can now use the amazing Smock framework :) 
 
Cheers
--
Sonni Nielsen


Reply all
Reply to author
Forward
0 new messages