View Full Version : Unit testing
jdmendoza
07-26-2005, 05:37 PM
Hello everyone! First of all I would like to congratulate the spring.net team for the GREAT work.
I'm having a little trouble with nant, nunit and spring.net... The structure I'm using a solution a core for the source code and a core.tests for the tests... the thing is that when I test with testdriven.net VS plug-in it works, but when I do it with nant, it fails... Does anyone have a demo project using nant, nunit, spring.net and nhibernate?
Rick Evans
07-26-2005, 08:56 PM
Hiya
I'd love to help, but could you be a little more specific about what exactly the issue is? For example, could you post the results of your failing NAnt build?
Ciao
Rick
jdmendoza
07-27-2005, 03:06 PM
Thanx for the quick response!
Here's the output:
[nunit2] Tests run: 5, Failures: 5, Not run: 0, Time: 0.7811232 seconds
[nunit2]
[nunit2] Failures:
[nunit2] 1) Financiero.Core.Tests.Security.RoleDAOTests.test_0 10_AddRole : Exception in configuration section handler (C:\desa\net\Fina
nciero.Net\build\net-1.1.win32\Financiero.Net-1.0.0-debug\bin\Financiero.Core.Tests.dll.config line 85)
[nunit2] at System.Configuration.ConfigurationRecord.EvaluateR ecursive(IConfigurationSectionHandler factory, Object config, String[]
keys, Int32 iKey, XmlTextReader reader)
[nunit2] at System.Configuration.ConfigurationRecord.EvaluateR ecursive(IConfigurationSectionHandler factory, Object config, String[]
keys, Int32 iKey, XmlTextReader reader)
[nunit2] at System.Configuration.ConfigurationRecord.Evaluate( String configKey)
[nunit2] at System.Configuration.ConfigurationRecord.ResolveCo nfig(String configKey)
[nunit2] at System.Configuration.ConfigurationRecord.GetConfig (String configKey)
[nunit2] at System.Configuration.DefaultConfigurationSystem.Sy stem.Configuration.IConfigurationSystem.GetConfig( String configKey)
[nunit2] at System.Configuration.ConfigurationSettings.GetConf ig(String sectionName)
[nunit2] at Financiero.Core.Tests.Security.RoleDAOTests.init() in c:\desa\net\Financiero.Net\test\Financiero\Financi ero.Core.Tests\S
ecurity\RoleDAOTests.cs:line 48
[nunit2]
[nunit2] 2) Financiero.Core.Tests.Security.RoleDAOTests.test_0 20_EditRole : Exception in configuration section handler (C:\desa\net\Fin
anciero.Net\build\net-1.1.win32\Financiero.Net-1.0.0-debug\bin\Financiero.Core.Tests.dll.config line 85)
[nunit2] at System.Configuration.ConfigurationRecord.EvaluateR ecursive(IConfigurationSectionHandler factory, Object config, String[]
keys, Int32 iKey, XmlTextReader reader)
[nunit2] at System.Configuration.ConfigurationRecord.EvaluateR ecursive(IConfigurationSectionHandler factory, Object config, String[]
keys, Int32 iKey, XmlTextReader reader)
[nunit2] at System.Configuration.ConfigurationRecord.Evaluate( String configKey)
[nunit2] at System.Configuration.ConfigurationRecord.ResolveCo nfig(String configKey)
[nunit2] at System.Configuration.ConfigurationRecord.GetConfig (String configKey)
[nunit2] at System.Configuration.DefaultConfigurationSystem.Sy stem.Configuration.IConfigurationSystem.GetConfig( String configKey)
[nunit2] at System.Configuration.ConfigurationSettings.GetConf ig(String sectionName)
[nunit2] at Financiero.Core.Tests.Security.RoleDAOTests.init() in c:\desa\net\Financiero.Net\test\Financiero\Financi ero.Core.Tests\S
ecurity\RoleDAOTests.cs:line 48
######################
######################
# Here's my App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<!--add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=localhost;initial catalog=test;User=jdmr;Password=admin2k1;Min Pool Size=2"
/-->
<add key="hibernate.dialect"
value="NHibernate.Dialect.MySQLDialect" />
<add key="hibernate.connection.driver_class"
value="NHibernate.Driver.MySqlDataDriver" />
<add key="hibernate.connection.connection_string"
value="Server=localhost;Database=test;User ID=root;Password=root;CharSet=utf8" />
</nhibernate>
<log4net debug="false">
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<file value="log.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100Kb" />
<rollingStyle value="Size" />
<staticLogFileName value="true" />
<param name="DatePattern" value="yyyy.MM.dd" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="rollingFile"/>
</root>
<logger name="NHibernate">
<level value="ERROR"/>
<appender-ref ref="console" />
</logger>
</log4net>
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net" >
<description>An example that demonstrates simple IoC features.</description>
<object name="roleDAO" type="Financiero.Core.Security.DAO.Hibernate.RoleDAOHibe rnate, Financiero.Core"/>
</objects>
</spring>
</configuration>
Rick Evans
07-27-2005, 03:46 PM
Hiya
Nope, I ain't seeing it straight off the bat, it all looks gravy to me. You say the tests all pass when you use the TestDriven.NET add-in, correct? It can't be a Spring config or NHibernate issue then.
Ok... does your NAnt script copy the App.config file into the build output directory? That is do you have a line somewhere in your buildfile that looks (roughly) like this...
<copy file="App.config" tofile="bin/Debug/Financiero.Core.Tests.dll.config"/>
The above XML snippet assumes the following: that your test assembly is called 'Financiero.Core.Tests.dll', that your output directory is 'bin/Debug', and that your application configuration file is sitting in the same directory as the build file, and that your application configuration file is called 'App.config'. Please amend as appropriate.
Ok, so after the execution of this task and the <csc/> task, you'll have an output directory containing your dlls, the config file and whatever else is part of your build (PDBs, images, etc). Run the build again.
Still failing, or you already had it like this? Dang :? Ok, does your <nunit/> task look (roughly) like this? Is the 'appconfig' attribute of the <nunit/> task present? Again, please amend the names as required, in this case I have again assumed that the assembly under test is contained within the file called 'Financiero.Core.Tests.dll'.
<nunit2>
<formatter type="Plain" />
<test assemblyname="bin/Debug/Financiero.Core.Tests.dll" appconfig="bin/Debug/Financiero.Core.Tests.dll.config" />
</nunit2>
Still failing? :? Dang. The exception reporting has been much improved over the release that you are using, because 'Exception in configuration section handler' is quite spectacularly useless. Ok I'm reaching here, but how about you put a try catch block around the code in the init() method of the RoleDAOTests class, and inside the catch block print out (to the console) the value of the exception's GetBaseException() method call? Dirty, real dirty, but if the code works in TestDriven.NET, and the other avenues are not helping... well, please post again including the results of the console output.
Shucks I'll be glad if someone else spots the issue here, 'cos if the first two suggestions don't work then I really am stumped.
Ciao
Rick
jdmendoza
07-27-2005, 06:51 PM
Hey... thanks for the tip... With it I found the real problem... it can't find the hbm.xml... I'm going to try and fix it...
Or if someone has an idea of what I'm doing wrong please let me know... I'll post the answer when I find it...
And thanks again for the tip...
test:
[nunit2] System.Configuration.ConfigurationException: Exception in configuration section handler (C:\desa\net\Financiero.Net\build\net-
1.1.win32\Financiero.Net-1.0.0-debug\bin\Financiero.Core.Tests.dll.config line 85) ---> Spring.Objects.FatalObjectException: Could not instantiate typ
e [Void .ctor(System.String[])]; is it an interface or an abstract class? Does it have a no-arg constructor? ---> System.Reflection.TargetInvocationEx
ception: Exception has been thrown by the target of an invocation. ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with nam
e 'roleDAO' defined in 'config [objects] line 95' : Initialization of object failed : Could not instantiate type [Void .ctor()]; is it an interface or
an abstract class? Does it have a no-arg constructor? ---> Spring.Objects.FatalObjectException: Could not instantiate type [Void .ctor()]; is it an i
nterface or an abstract class? Does it have a no-arg constructor? ---> System.Reflection.TargetInvocationException: Exception has been thrown by the t
arget of an invocation. ---> NHibernate.MappingException: Resource: Financiero.Core.Security.Model.Role.hbm.xml not found
[nunit2] at NHibernate.Cfg.Configuration.AddClass(Type persistentClass)
[nunit2] at Financiero.Core.Security.DAO.Hibernate.RoleDAOHibe rnate..ctor() in c:\desa\net\Financiero.Net\src\Financiero\Financie ro.
Core\Security\DAO\Hibernate\RoleDAOHibernate.cs:li ne 46
[nunit2] --- End of inner exception stack trace ---
jdmendoza
08-03-2005, 03:17 PM
As you can see in my last post I have the Role.hbm.xml missing... in visual studio I have it as an embedded resource, that's the reason why it works with the testdriven plugin... All I had to do was embed the resource in the nant build file :wink: .
That's it!
Hope it helps someone...
David
Mark Pollack
08-03-2005, 05:34 PM
Hi David,
Glad you figured it out! The solution task does handle this stuff better than the csc task, though you do loose some control and independence from the .sln settings if you use it.
Cheers,
Mark
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.