How to Test assemble api in JcrPackageManager in Junit test cases - aem

I am writing test cases for content backup service in AEM 6.0 using assemble(JcrPackage pack, ProgressTrackerListener listener) API in JcrPackageManager class. How can I test whether package is assembled or not in my JUnit test cases?

If your unit tests are executed in an AEM environment (1) you might check if the package is existing in the expected repository path (/etc/packages/GROUP/PACKAGE) and if the size is > 0 (jcrPackage.getSize()).
Be aware that this will not work with
assemble(JcrPackage pack, ProgressTrackerListener listener, OutputStream out)
as the package will not be stored in the repo after the assembly has been done. It will be streamed to the output stream.
(1): https://sling.apache.org/documentation/development/sling-testing-tools.html

Related

How to debug unit test while developping a package in Julia

Say I develop a package with a limited set of dependencies (for example, LinearAlgebra).
In the Unit testing part, I might need additional dependencies (for instance, CSV to load a file). I can configure that in the Project.toml all good.
Now from there and in VS Code, how can I debug the Unit tests? I tried running the "runtests.jl" in the debugger; however, it unsurprisingly complains that the CSV package is unavailable.
I could add the CSV package (as a temporary solution), but I would prefer that the debugger run with the configuration for the unit testing; how can I achieve that?
As requested, here is how it can be reproduced (it is not quite minimal, but instead I used a commonly used package as it give confidence the package is not the problem). We will use DataFrames and try to execute the debugger for its unit tests.
Make a local version of DataFrames for the purpose of developing a feature in it. I execute dev DataFrames in a new REPL.
Select the correct environment (in .julia/dev/DataFrames) through the VS-code user interface.
Execute the "proper" unit testing by executing test DataFrames at the pkg prompt. Everything should go smoothly.
Try to execute the tests directly (open the runtests.jl and use the "Run" button in vs-code). I see some errors of the type:
LoadError: ArgumentError: Package CategoricalArrays not found in current path:
- Run `import Pkg; Pkg.add("CategoricalArrays")` to install the CategoricalArrays package.
which is consistent with CategoricalArrays being present in the [extras] section of the Project.toml but not present in the [deps].
Finally, instead of the "Run" command, execute the "Run and Debug". I encounter similar errors here is the first one:
Test Summary: | Pass Total
merge | 19 19
PASSED: index.jl
FAILED: dataframe.jl
LoadError: ArgumentError: Package DataStructures not found in current path:
- Run `import Pkg; Pkg.add("DataStructures")` to install the DataStructures package.
So I can't debug the code after the part requiring the extras packages.
After all that I delete this package with the command free DataFrames at the pkg prompt.
I see the same behavior in my package.
I'm not certain I understand your question, but I think you might be looking for the TestEnv package. It allows you to activate a temporary environment containing the [extras] dependencies. The discourse announcement contains a good description of the use cases.
Your runtest.jl file should contain all necessary imports to run tests.
Hence you are expected to have in your runtests.jl file lines such as:
using YourPackageName
using CSV
# the lines with tests now go here.
This is a standard in Julia package layout. For an example have a look at any mature Julia such as DataFrames.jl (https://github.com/JuliaData/DataFrames.jl/blob/main/test/runtests.jl).

How can I get the build output directory inside tests run by sbt?

I need to create directories and files for some tests. My project uses sbt as the build tool, and common practice is to use File.createTempFile or similar APIs, but I abhor that practice. I want all files created by my tests to reside somewhere inside the output directory (<module>/target/), so that they'll be removed when I run clean, but otherwise preserved if I have need of them to figure out test failures.
The test framework is not relevant: if your solution requires a particular framework, I'll happily adopt it or figure out how it does the trick and use that.
In short, I need the answer to one of these two questions:
How can I create a file inside the build output directory from a test run by sbt?
How can I find out what is the build output directory for the current project from a test run by sbt?
In ScalaTest, try passing target
settingKey[File]("Main directory for files generated by the build.")
to config map as -Dkey=value. For example, in build.sbt specify
Test / testOptions += Tests.Argument(s"-DtargetDir=${target.value}")
and then define test like so
import org.scalatest._
class ExampleSpec extends fixture.FlatSpec with fixture.ConfigMapFixture with Matchers {
"The config map" should "contain target directory used by sbt" in { configMap =>
configMap should contain key "targetDir"
}

Taurus NUnit runner not finding tests

I have a simple NUnit test that makes a simple WebAPI call:
[TestFixture]
public class PerformanceTests
{
private const string web_api = <myapiurl>;
[Test]
public async Task PerformanceTest()
{
var response = await client.GetAsync(web_api);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
The test runs fine in Visual Studio when run using the normal test runner and also via ReSharper; it is highlighted as a NUnit test.
I have Taurus installed and have created a simple yml file to run my test:
execution:
- executor: nunit
iterations: 500
scenario:
script: C:\Users\...\tests.dll # assembly with tests
When I run the yml file in Taurus:
bzt my-nunit-tests.yml
the tests do not run and I get the following output:
Target: C:\Users\...\tests.dll
15:36:51 ERROR: NUnitExecutor STDERR:
Unhandled Exception: System.ArgumentException: Nothing to run, no tests were loaded
at NUnitRunner.NUnitRunner.Main(String[] args)
It would seem that the custom Taurus NUnit test runner is not picking up the tests. I can run the tests with the standard dotnet test command (the project does have the Microsoft.NET.Test.Sdk as a dependency).
As my test project is .NET Core I am publishing the project to ensure all dependencies are included. This ensures all the files specified here are in the same directory as the test assembly.
Update: I have created the exact same test project but in .NET Framework and Taurus finds the tests. To me this suggests the custom Taurus NUnit Test Runner doesn't work with .NET Core projects but I can't confirm this.
As of June 2018 Taurus' Custom NUnit runner does not support .NET Core. It would seem that this is because NUnit does not allow .NET Core tests to be run via the .NET Framework engine.

Moles hosting unable to resolve dependent assembly

I'm developing a test method where both RhinoMocks as well as Moles are used. I have declared the host type as Moles for the test method as required. Now when it comes to the point where RhinoMocks has to emit a runtime assembly to create a mock for an interface, a FileNotFoundException is thrown, pointing to one of my private DLLs, which infact resides in the same folder as the test DLL and the DLL containing the class under test.
But when I remove the HostType attribute, the creation of mock instance works just fine, without any exceptions.
So, I built a workaround to get this to work with the Moles host type using these following steps:
1) I registered to the AppDomain.CurrentDomain.AssemblyResolve event in my [TestInitialize]
2) In the event handler, I packed in the following logic:
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string asmPath = Environment.CurrentDirectory + "\\" + args.Name.Split(',')[0] + ".dll";
if(System.IO.File.Exists(asmPath))
return System.Reflection.Assembly.Load(new System.Reflection.AssemblyName(args.Name));
return null;
}
Now, as expected, this assembly resolver does get invoked when the test is executed under the Moles HostType. The test now runs.
But the questions still are:
1) why do I still need a separate assembly resolver even if we explicitly set the AppDomain's APPBASE path to the folder containing all my production DLLs and the test DLL using
AppDomain.CurrentDomain.SetData("APPBASE", Environment.CurrentDirectory);
where I've checked that the current directory is indeed my bin folder where every binary, including the test DLL, can be found.
2) Why do I not encounter this error when I'm testing some other production DLL of mine, where the same dependencies are involved.
3) This problem occurs also when using NMock2, which I tried just to see if this was a RhinoMocks-specific problem. But turns out that even NMocks2 has the same problem in the Reflection.Emit step, with the same dependent DLL as with RhinoMocks. Any way to explain this?
The part you are missing is that when you run the unit test the app which is running is the unit test hosts the application. The CurrentDirectory of that application is probably the directory in which its runner is installed. Your assemblies are passed in with parameters to the application.
The assumption that the CurrentDirectory is the directory of your application is faulty, this explains points 1, 2 and 3.

Teamcity not DLL's for some NUnit Test projects

I get this error when running my Moq tests through Teamcity 5
Test(s) failed.
System.IO.FileNotFoundException :
Could not load file or assembly 'Moq,
Version=3.1.416.3, Culture=neutral,
PublicKeyToken=69f491c39445e920' or
one of its dependencies. The system
cannot find the file specified. at
MyCode.Tests.SomeHandlerTests.Setup()
The tests run fine on my local; they just fail on the build server.
I made sure the assemblies are in the Bin (looking at them now over RDP just be double sure).
So the issue was to do with the Test DLL search path under the nunit settings
It was:
..\Tests\**\*Test*.dll
But is now:
..\Tests\*\bin\Debug\*Test*.dll
And things work nicely
UPDATE
http://confluence.jetbrains.com/display/TCD8/NUnit
You can use this pattern
**\*.dll
as long as you add this pattern in the "Do not run tests from" field
**\obj\**\*.dll
I had a similar issue, but found that I had different version's of Moq between my 2 Test projects.
The issue that I had was that the correct version was not available.
Just do
Update-Package Moq
From the Package Manager command line