例外スロー時のテスト方法

【例外スロー時のテスト】

■クラス生成
1.上記記述の『CFUnitExample』(CFML)プロジェクトにMyComponent.cfcを追加する。
<cfcomponent name="MyComponent">
 
  <cfproperty name="entities" type="array">
 
  <!--- Initialization --->
  <cffunction name="init" access="public" returntype="MyComponent" output="No">
    <cfset setEntities( ArrayNew( 1 ) )>
    <cfreturn THIS>
  </cffunction>
 
  <cffunction name="throwError" access="public" returntype="void" output="No">
    <cfargument name="doThrow" required="Yes" type="boolean">
   
    <cfif ARGUMENTS.doThrow>
      <cfthrow message="Testing CFThrow in CFUnit" type="TestThrow">
    </cfif>
   
  </cffunction>
 
</cfcomponent>

2. 『CFUnitExample』(CFML)プロジェクトにTestMyComponent.cfcを追加する。

<cfcomponent name="TestMyComponent" extends="net.sourceforge.cfunit.framework.TestCase">

  <cffunction name="testThrowError" returntype="void" access="public">
    <cfset var b = false>
   
    <cftry>
      <!--- Generate an error --->
      <cfinvoke component="MyComponent" method="throwError">
        <cfinvokeargument name="doThrow" value="true">
      </cfinvoke>
     
      <!--- Catch error --->
      <cfcatch type="TestThrow">
        <cfset b = true>
      </cfcatch>
    </cftry>
   
    <!--- Verify error was thrown --->
    <cfset assertTrue("Expected error was not thrown", b)>
   
  </cffunction>
 
</cfcomponent>

3.『CFUnitExample』(CFML)プロジェクトにindex.cfmを追加する。
<cfsilent>
  <cfset CFUnitRoot = "net.sourceforge.cfunit" />
 
  <cfset tests = ArrayNew(1)>
  <cfset ArrayAppend(tests, "CFUnitExample.TestMyComponent")>
  <cfset testsuite = CreateObject("component", "#CFUnitRoot#.framework.TestSuite").init( tests )>
</cfsilent>

<cfoutput>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
  <html>
  <head>
    <title>CFThrow Test Example</title>
  </head>
 
  <body>
  <h1>Collection Test Example</h1>
  <hr>
  <h2>CFUnit Test</h2>
  <cfinvoke component="#CFUnitRoot#.framework.TestRunner" method="run">
    <cfinvokeargument name="test" value="#testsuite#">
    <cfinvokeargument name="name" value="">
  </cfinvoke>
  </body>
  </html>
</cfoutput>

最終更新:2007年01月15日 18:07
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。