SpringUOJ/Test

HomePage | SpringUOJ | RecentChanges | Preferences

An attempt to test the Spring UOJ code using EasyMock? Class Extension.

See test results, below.

/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.asu.vpia.springuoj;

import junit.framework.TestCase;
import asjava.uniobjects.UniSession;
import asjava.uniobjects.UniFile;
import asjava.uniclientlibs.UniDynArray;
import asjava.uniclientlibs.UniString;
import org.easymock.classextension.MockClassControl;

/**
 * <p>Mock object based tests for UojTemplate</p>
 * <p>This class is based on org.springframework.jdbc.core.JdbcTemplateTests
 * by Rod Johnson and Thomas Risberg</p>
 */
public class UojTemplateTest extends TestCase {

    protected MockClassControl ctrlUniSession;
    protected UniSession mockUniSession;

    protected MockClassControl ctrlUniFile;
    protected UniFile mockUniFile;

    protected void setUp() throws Exception {
        super.setUp();

        // Can't seem to mock a UniFile:
        //    net.sf.cglib.core.CodeGenerationException: asjava.uniobjects.UniFileException-->
        //    Cannot instantiate this object in this manner.  Must use the UniSession object
        // set up the mock UniFile
        ctrlUniFile = MockClassControl.createControl(UniFile.class, null, null);
        mockUniFile = (UniFile) ctrlUniFile.getMock();

        // teach the mockUniFile to return a dynamic array when read is called with a key
        mockUniFile.read("key");
        ctrlUniFile.setReturnValue(new UniString("record"));

        // set up the mock UniSession
        ctrlUniSession = MockClassControl.createControl(UniSession.class, null, null);
        mockUniSession = (UniSession) ctrlUniSession.getMock();

        // teach the mock UniSession to return the EasyMock mockUniFile when openFile is called
        mockUniSession.openFile("filename");
        ctrlUniSession.setReturnValue(mockUniFile);

        // teach the mock UniSession to return a MockUniFile when openFile is called
        mockUniSession.openFile("filename");
        ctrlUniSession.setReturnValue(new MockUniFile());

        // step out of training mode
        ctrlUniSession.replay();
        ctrlUniFile.replay();

    }

    public void testRead() throws Exception {

        UojTemplate ut = new UojTemplate();
        ut.setUniSession(mockUniSession);

        UniDynArray ud = ut.read("filename", "key");
        assertEquals("record", ud.toString());

    }


    protected void tearDown() throws Exception {
        super.tearDown();

        ctrlUniSession.verify();
        ctrlUniFile.verify();

    }

}

Test Results

-------------------------------------------------------------------------------
Test set: edu.asu.vpia.springuoj.UojTemplateTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.395 sec <<< FAILURE!
testRead(edu.asu.vpia.springuoj.UojTemplateTest)  Time elapsed: 0.347 sec  <<< ERROR!
net.sf.cglib.core.CodeGenerationException: asjava.uniobjects.UniFileException-->Cannot instantiate this object in this manner.  Must use the UniSession object
	at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)
	at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:220)
	at net.sf.cglib.proxy.Enhancer.createUsingReflection(Enhancer.java:604)
	at net.sf.cglib.proxy.Enhancer.firstInstance(Enhancer.java:532)
	at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
	at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:373)
	at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:300)
	at org.easymock.classextension.MockClassControl$ClassProxyFactory.createProxy(MockClassControl.java:81)
	at org.easymock.MockControl.<init>(MockControl.java:125)
	at org.easymock.classextension.MockClassControl.<init>(MockClassControl.java:356)
	at org.easymock.classextension.MockClassControl.<init>(MockClassControl.java:367)
	at org.easymock.classextension.MockClassControl.createControl(MockClassControl.java:177)
	at edu.asu.vpia.springuoj.UojTemplateTest.setUp(UojTemplateTest.java:46)
	at junit.framework.TestCase.runBare(TestCase.java:125)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:118)
	at junit.framework.TestSuite.runTest(TestSuite.java:208)
	at junit.framework.TestSuite.run(TestSuite.java:203)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:210)
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:135)
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:122)
	at org.apache.maven.surefire.Surefire.run(Surefire.java:129)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:585)
	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:225)
	at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:747)
Caused by: asjava.uniobjects.UniFileException: Cannot instantiate this object in this manner.  Must use the UniSession object
	at asjava.uniobjects.UniFile.<init>(UniFile.java:49)
	at asjava.uniobjects.UniFile$$EnhancerByCGLIB$$68c3b937.<init>(<generated>)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
	at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:228)
	... 33 more

HomePage | SpringUOJ | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited November 13, 2006 8:44 am by WendySmoak (diff)
Search: