How do I create Duplicate/Matching Rules through Apex code for an Apex test class? - unit-testing

I recently wrote an API to implement Account/Contact Duplicate/Matching rules for a connected app. It all works fine.
It does the same thing that Salesforce does for these rules. It pretty much follows this example code: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Datacloud_DuplicateResult.htm
I'm now trying to write a test class for this but I'm hitting a roadblock.
I need a way to do one of two things:
Create Duplicate and Matching rules through Apex code within the test. I would create them, run the test, then delete them.
Turn Duplicate and Matching rules on and off through Apex code within the test. Same general idea but I could create them beforehand in our testing org.
As far as I can tell there is no way to do this. Am I missing something here?
If this isn't possible then how do I get test coverage on my class. It will only get full code coverage if the call to Database.Insert actually fails with DuplicateErrors.
Edit: I should add that having rules always on and bypassing them with DMLHeader for testing is not an option.

I would say you don't have to create the Duplicate or Matching rules in Apex (not even sure if it is possible). In fact, you should rely on having the Duplicate rules that you've configured in Salesforce. So you should just try to generate the duplicate Salesforce as the Duplicate Rules should trigger also when running the test.

Related

Unit Testing Bot Framework

I'm trying to figure out how to unit test a basic MS Bot Framework dialog and cannot get it to work the way everything on the internet says it should work.
Everything I find basically says follow this:
https://github.com/Microsoft/BotBuilder/blob/master/CSharp/Tests/Microsoft.Bot.Sample.Tests/EchoBotTests.cs
Well, here's the problem with that:
await Conversation.SendAsync(scope, toBot);
That is defined as internal so it is not accessible outside of the bot.builder code. So it is totally useless unless you are programming tests for internal bot.builder stuff.
Is there a new way to get around this?
Bot Framework is an open source project, you can download the code and modify it as you need. In your case removing the internal keyword. Another option would be to create a new class that inherits from the class you are trying to use and making your own access levels on the methods you need to override. This blog post describes how to use the code locally.

Can rules be used to run certain test methods

I have question related to creating test methods in my coded UI test.
Is it possible to create like rules or like an if else statement tree that would execute certain test methods to run when certain things happening or when i'm on a certain counter?.
I don't know if this is the correct way to do this. I was going to do it in a 1 huge block of code but i dont really like the direction that is going in, since the application i'm testing has to account for different paths.
I want to create and have test methods run based on these if else statements code blocks.
If anyone has done this any help would be greatly appreciated. Thanks
I think you want your tests to be as small as possible and test only one thing.
For multiple reasons you want to test in isolation and not make tests depended on each other. What you are trying todo sounds very unmaintainable on the long run.
Also when a single test steps fails, you might need to run a long test to see the actual fail, making debuging harder and slower.
I would follow the AAA pattern in your tests to guide you
Arrange: Setup the state of the application including data
Act: The action you want to test. (e.g fill in a form and submit)
Assert: Verify the action was correct by checking one thing
It is possible to group tests and run only a subset, for details see: https://msdn.microsoft.com/en-us/library/dd286683.aspx

WatiN - what to test?

I have been writing a lot of unit tests for the code I write. I've just started to work on a web project and I have read that WatiN is a good test framework for the web.
However, I'm not exactly sure what I should be testing. Since most of the web pages I'm working on are dynamic user generated reports, do I just check to see if a specific phrase is on the page?
Besides just checking if text exists on a page, what else should I be testing for?
First think of what business cases you’re trying to validate. Ashley’s thoughts are a good starting point.
You mentioned most pages are dynamically generated user reports. I’ve done testing around these sorts of things and always start by figuring out what sort of baseline dataset I need to create and load. That helps me ensure I can get exactly the appropriate set of records back in the reports that I expect if everything's working properly. From there I’ll write automation tests to check I get the right number of records, the right starting and ending records, records containing the right data, etc.
If the reports are dynamic then I’ll also check whether filtering works properly, that sorting behaves as expected, etc.
Something to keep in mind is to keep a close eye on the value of these tests. It may be that simply automating a few tests around main business use cases may be good enough for you. Handle the rest manually via exploratory testing.
You essentially want to be testing as if you are a user entering your site for the first time. You want to make sure that every aspect of your page is running exaclty the way you want it to. For example, if there is a signup/login screen, automate those to ensure that they are both working properly. Automate the navigation of various pages, using Assertions just to ensure the page loaded. If there are generated reports, automate all generations and check the text on the generations to ensure it is what you specified by the "user" (you). If you have any logic saying for example when you check this box all other boxes should check aswell. There are many assertions that can be applied, I am not sure what Unit-Testing software you are using but most have a very rich assortment.

Could not find the ColdFusion Component or Interface Answer.

I sometimes get
Could not find the ColdFusion Component or Interface Answer.
and simply doing a refresh fixes the problem.
This is not case where the program is being refreshed from ftp while I try to browse from it: no development is being done. But every once in a while I'll get it while trying to do a createobject.
Q: Is there a best practice for sleeping and trying again if instantiating a component fails?
Are you using a cluster of servers? If so perhaps you have one server misconfigured - perhaps a missing mapping - and when you get served from that server you see the issue? That might explain the way it seems to only sometimes happen.
By the way, instansiation of CFCs should always work - you should not be trying to code around this issue by sleeping and trying again.
Phillip, any chance this is on cf 9.0.1, and you're using the "import" keyword?
The reason I ask is that I've seen behavior -- and logged a bug report on it -- where if I have two different object creations on the same page (or in another CFC... doesn't matter), both from the same package, and I'm not using the fully qualified CFC name but instead am using import, then the first createObject() will succeed and the second will fail with the "could not find ... " error.
I wonder if something like that could be at work here.

How to unit-test a NextPasswordChangeDate function against the Active Directory

I am working on a project using the Active Directory, intensively. I set up a few unit tests for several things against the AD, some of which I achieve using mocked objects, some which I achieve through real calls against the AD.
As one of the functions of my project, I have to retrieve a so called "user profile". This user profile consists mostly of simple attributes, like "cn", "company", "employeeid", etc. However, one property that I am trying to fill is not a simple one "NextPasswordChangeDate".
To the best of my knowledge, the only way to get this, is by getting the domain policy's maxPwdAge and use this information together with pwdLastSet.
Now my question: How can I unit test this in an intelligent way? I came up with three options, all of which are not great:
Use my own account as the searched account, find out the date by other means and hard code it in the unit test. By this way, I can unit test my code well, but every month, I have to change the unit test, because I changed my password.
Use some account that has password never expires set. This is kind of pointless, because I cannot really test the correctness of my code by that.
Use a mock object and make sure that the correct API calls happen. This option allows to test the correctness of the function's behaviour, but then the tested logic is in fact in the unit test and hence I cannot be sure, that it is doing the right thing, even if the test is passed.
Which of the three do you suggest? Or maybe you have a better option?
Since 1 and 2 the rely on AD existing and having known values seem more like integration tests to me.
I generally take the side that any non-deterministic behavior should be interfaced out and mocked if possible (#3). As you noted this will always leave an amount of real implementation code that is not unit-testable, but would then be covered by your integration tests running against a known AD system.
Related Question/Answer