Announcement Announcement Module
Collapse
No announcement yet.
get the TestContext? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • get the TestContext?

    Hi, guys, i have a difficulty in obtaining current TestContext when my test runner is SpringJUnit4ClassRunner. I want to get current test method name via TestContext. So could anyone suggest me a way, or Spring has not opened it to outside access?

    Cheers,
    Cheng Wei

  • #2
    From which place do you want to get it? from test?

    Comment


    • #3
      Hi Andrei, Thanks for your reply. Yes, i want to get it inside my unit tests.

      Comment


      • #4
        Did you try

        Code:
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations = { "classpath:my-context.xml"})
        public class MyTest {
        	@Autowired
        	private ApplicationContext applicationContext;
        	
        	@Test
        	public void myTest() {
                    //do something with context
        	}
        }

        G

        Comment


        • #5
          As I understand cheng wants to get TestContext.

          It is possible to write cutome TestExecutionListener and set test method to test instance:

          Code:
          public class CustomTestExecutionListener extends AbstractTestExecutionListener {
          
              @Override
              public void beforeTestMethod(final TestContext testContext) throws Exception {
                  final BaseTest testInstance = (BaseTest) testContext.getTestInstance();
                  testInstance.setTestMethod(testContext.getTestMethod());
              }
          
          }
          Code:
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(locations = {
                  "/ctx/applicationContext-test.xml"
          })
          @TestExecutionListeners(CustomTestExecutionListener.class)
          public class BaseTest extends AbstractTransactionalJUnit4SpringContextTests {
          
              private Method testMethod;
          
              public Method getTestMethod() {
                  return testMethod;
              }
          
              public void setTestMethod(final Method testMethod) {
                  this.testMethod = testMethod;
          
              }
          }
          Code:
          public class CustomTest extends BaseTest {
          
              @Test
              public void someTest() {
                  Assert.assertEquals(ReflectionUtils.findMethod(CustomTest.class, "someTest"), getTestMethod());
              }
          
          }

          But is it make sense to know current executed method from itself?
          Last edited by Andrei Tsibets; Dec 15th, 2008, 05:36 AM.

          Comment


          • #6
            Thanks Goran and Andrei. Yes, Andrei, that is exactly what i want. i didn't notice there was such Listener(AbstractTestExecutionListener) in spring test package.

            The purpose to know current executed method from itself can be various, but in my case, i want to get testing data for my functional tests and the file names of those data are named as the same as test method names. Surely you can pass in the name hardcoded.

            And of course, someone else requested the feature when Junit4 came out and Joshua.Graham from thoughtworks.com has implemented a handy class achieving it. see the link below:
            Code:
            http://code.google.com/p/twgeeknight/source/browse/trunk/JUnit4Playground/src/org/junit/runners/NameAwareTestClassRunner.java?r=88
            Cheers,
            Cheng Wei

            Comment


            • #7
              Hi Cheng,

              FYI: please note that Joshua Graham's NameAwareTestClassRunner is a custom JUnit Runner, and as such it (typically) cannot be used in conjunction with the Spring TestContext Framework, since you cannot use two different runners at the same time. Thus, as Andrei pointed out, with the TestContext framework the best solution is to write a custom TestExecutionListener.

              If you're looking for an example, you might find the DataSetTestExecutionListener class in the DBUnit integration from Carbon Five useful.

              Regards,

              Sam

              Comment


              • #8
                Hi Sam, Thanks. No, they can't be used together. A customized TestExecutionListener is the solution for SpringJUnit4ClassRunner. And my implementation is as follows:
                Code:
                public class TestContextListener extends AbstractTestExecutionListener 
                {
                  @Override
                  public void beforeTestMethod(TestContext testContext) throws Exception 
                  {
                    Object testInstance = testContext.getTestInstance();
                    if(TestContextAware.class.isInstance(testInstance))
                    {
                      ((TestContextAware)testInstance).setTestContext(testContext);
                    }
                    super.beforeTestMethod(testContext);
                  }
                }
                Code:
                public interface TestContextAware 
                {
                  void setTestContext(TestContext testContext);
                }
                Code:
                @RunWith(SpringJUnit4ClassRunner.class)
                ...
                @TestExecutionListeners({TestContextListener.class,OtherListeners.classes})
                public class ClassAwareOfTestContext implements TestContextAware
                {
                  ...
                }
                Cheers,
                Cheng Wei
                Last edited by cheng; Dec 16th, 2008, 08:28 PM.

                Comment

                Working...
                X