Passing dataProvider parameter inside testng method - maven

I have two #DataProviders:
#DataProvider(name = "smallNumbers")
#DataProvider(name = "bigNumbers")
Variables in pom.xml
<systemPropertyVariables>
<dataP>${dataProvider}</dataP>
</systemPropertyVariables>
Accesing parameter:
String sizeNumbers = System.getProperty("dataP");
And my test:
#Test(dataProvider=sizeNumbers)
dataProvider in test method have to be:
a constant expression
Any idea how to pass variable inside #Test(dataProvider= ?

You cant do this. It is only possible to pass dataprovider directly to method.
Why you choose that way to inject data from dataprovider? Show a bit more about your code architecture cause that's look strange.
EDIT:
You can check this way:
#DataProvider(name = "dp")
public static Object[][] dataInject(){
return new Object[][]{
{sizeNumbers}
};
}
And inside "dp" you can also make some validation for ex. "isNull" etc.
Then in test
#Test(dataProvider = "dp", dataProviderClass = Xyz.class)
public void testFirst(String input){
//...
}

Related

MapStruct Spring Page to custom object conversion includes check

I am using MapStruct to convert a Page object to a custom object of my application. I am using this mapping in order to convert the content field of the Page object to a list of custom objects found in my data model:
#Mapping(target = "journeys", source = "content")
While this works OK and does convert the elements when content is present, this does not work correctly in case of no Page content. Taking a look at the code seems to show that the following check is added in the generated mapper class:
if ( page.hasContent() ) {
List<JourneyDateViewResponseDto> list = page.getContent();
journeyDateViewPageResponseDto.setJourneys( new ArrayList<JourneyDateViewResponseDto>( list ) );
}
When this is added the mapping action of the inner objects is omitted, meaning that I end up with a null list. I am not really sure as to why and how this check is added but I would like to find a way of disabling it and simply end up with an empty list of elements. Is there a way this can be done using MapStruct?
MapStruct has the concept of presence checkers (methods that have the pattern hasXXX). This is used to decide if a source property needs to be mapped.
In case you want to have a default value in your object I would suggest making sure that your object is instantiated with an empty collection or provide an #ObjectFactory for your object in which you are going to set the empty collection.
e.g.
Default value in class
public class JourneyDateViewPageResponseDto {
protected List<JourneyDateViewResponseDto> journeys = new ArrayList<>();
//...
}
Using #ObjectFactory
#Mapper
public interface MyMapper {
JourneyDateViewPageResponseDto map(Page< JourneyDateViewResponseDto> page);
#ObjectFactory
default JourneyDateViewPageResponseDto createDto() {
JourneyDateViewPageResponseDto dto = new JourneyDateViewPageResponseDto();
dto.setJourneys(new ArrayList<>());
return dto;
}
}
#Mapping(target = "journeys", source = "content", defaultExpression = "java(java.util.List.of())")

Understanding static methods inside the Validation class in angular4

i am starting to learn model driven forms in angular and when i was going through the documentation of the model driven forms i found this
component.ts
this.myForm= this.fb.group({
'contact':['',Validators.required]
});
now when i went to the definition of the validator class i found this
export declare class Validators {
...
static required(control: AbstractControl): ValidationErrors | null;
...
}
which explains required is a static method in the validator class and it requires a AbstractControl as a parameter. but why then i am allowed to use it without passing any parameter inside it.
The required method returns an error map with the 'required' property: {'required':true} if the value of control: AbstractControl is empty and null if its not.
.
From the angular source code: https://github.com/angular/angular/blob/6.1.9/packages/forms/src/validators.ts#L133-L154
static required(control: AbstractControl): ValidationErrors|null {
return isEmptyInputValue(control.value) ? {'required': true} : null;
}
.
The reason why you can pass Validators.required without parenthesis and parameters is because Typescript is a superset of Javascript, which can store functions as variables:
var Foo = function (control: AbstractControl)
{
return anyVal;
};
Is the same as:
Foo(control: AbstractControl): any
{
return anyVal;
};
So doing this is completely valid
var Bar = Foo;
And because a function is just a variable holding executable code, we can store the function in multiple variables or pass it as a parameter, which is what is done in the FormControl.
So basically, when you do
const control = new FormControl('', Validators.required);
You are not executing the required method, because a method is executed only when parenthesis and parameters are added. Instead, you are passing the Validator function itself.

NSubstitute if-else condition

Good day! I need your help, I have next tests:
[SetUp]
public void SetUp()
{
controller = Substitute.For<IApplicationController>();
view = Substitute.For<ICamerasView>();
presenter = new CamerasPresenter(controller, view);
argument = InitializeDevicesList();
presenter.Run(argument);
}
private List<string> InitializeDevicesList()
{
List<string> devicesList = new List<string>();
Device device = new Device();
devicesList.Add(device.Name);
return devicesList;
}
[Test]
public void RunIfDeviceListIsNotEmpty()
{
view.DidNotReceive().SetUIOnNoConnectedDevices();
view.Received().FillCamerasListView(argument);
view.Received().Show();
}
which actually tests next code
public override void Run(List<string> argument)
{
connectedCameras = argument;
if(connectedCameras.Count == 0)
{
SetUIOnNoConnectedDevices();
}
else
{
FillCamerasListView();
}
View.Show();
}
And my issue is that FillCamerasListView method isn't calling in test. But as it expected it called in Run method in this case. So, I can't imagine what is the problem, so I will be very appreciated for your help. Thanks for your time!
This example passes. The problem appears to be something in your example that is changing the argument passed to FillCamerasListView as discussed in the comments.
A few options:
Modify the code to match the test's expectation. i.e. pass the argument given to Run on to FillCamerasListView.
Use view.ReceivedWithAnyArgs().FillCamerasListView(null) to assert a call was made without worry about the specifics of the argument passed.
Use view.Received().FillCamerasListView(Arg.Is<List<string>>(x => Matches(x, argument)), where Matches is your own code which determines whether the argument given is correct based on the argument passed to Run.

How to generate comment inside a method with JCodeModel

I need something like this
public void method() {
//TODO generated sources
}
Here is how I generate a class and a method
JCodeModel cm = new JCodeModel();
JDefinedClass dc = cm._class("MyClass");
JMethod method = dc.method(JMod.PUBLIC, cm.VOID,"method");
adding method.body().directStatement("//TODO generated sources"); worked

Mocking MailController with ActionMailer.net using Moq

I'm trying to mock my MailController
_mockMailController = new Mock<IMailController>();
_mockMailController.Setup(x => x.ForgotPassword("test#email.com"));
My controller takes an IMailController as a dependancy, however when I call
mailController.ForgotPassword("test#email.com").Deliver();
I get a NullReferenceException (because ForgotPassword doesn't return anything, I guess)
Ideally, we'd stub EmailResult ?
I created a pull request for the ActionMailer.Net that intoduces an IEmailResult interface that makes mocking very easy. Have a look at this:
https://bitbucket.org/swaj/actionmailer.net/pull-request/4/iemailresult-interface-for-better/
Until the pull request is merged you could use a custom build from my frok of the project.
https://bitbucket.org/hydr/xv-actionmailer.net
Mocking get's as easy as writing (with FakeItEasy, Moq might be similar):
//SetUp
_myMailer = A.Fake<IMyMailer>();
//Later on in Assert
A.CallTo(() => _myMailer.MyTestEmail()).MustHaveHappened(Repeated.Exactly.Once);
when the Mailer is defined like:
public class MailController : MailerBase, IMyMailer
{
public IEmailResult MyTestEmail()
{
To.Add("recipient#sdf.com");
From = "sender#sdf.com";
Subject = "Subject";
return Email();
}
}
You have not created a setup for the mock return a value from ForgotPassword method. With default behavior this will return default value for the type, which is null in this case.
You can mock the return value like this:
_mockMailController.Setup(x => x.ForgotPassword("test#email.com"))
.Returns(new SomeType());

Resources