How to say any Integer or Float along with custom parameter - mockito

I have method with following signature
public List<VwCmsListCaseStatus> fetchOpenCase(Long partyId, Long partyEventTypeId, Integer page, Integer pageLength, List<ColumnSortInfo> columnSortInfo);
I am trying to do when().thenReturn() but not sure how to say return me object nomatter parameter value are. I tried eq(Long.class) as well as anyLong() but none is working. So definitely I am missing something here.

Take a look to the following example
import org.mockito.Matchers.*;
#Test
public void testSomething() {
Mockito.when(userRepository.getPassword(anyString())).thenReturn("PasswordMock");
userService.validateCredential(username);
Mockito.verify(userRepository).getPassword(username);
}

Related

How to implement setText to integer dataType in Kotlin?

I want to set data from my editText which has integer typedata. if it is string, we can make setTake, but how we can implement it at integer?
The warning is
None of the following functions can be called with the arguments supplied.
setText(CharSequence!) defined in com.google.android.material.textfield.TextInputEditText
setText(Int) defined in com.google.android.material.textfield.TextInputEditText
Your code shows data is of type UserDto. I don't know what that class looks like but it has a field called phoneNumber. This is what you are passing to setText(). I can't tell what type this phoneNumber is because you haven't shown us your UserDto class but the error tells us that it's neither an Int nor CharSequence, which it needs to be. You can try to do this maybe to make it work
binding.etPhone.setText(data.phoneNumber.toString())

Groovy Attribute should have type 'java.lang.Integer', but found type 'java.lang.Object'

I have the following JUnit test:
public class JavaTest {
final int value = 2;
#Test
#Repeat(times = value)
public void test() {
fail("Not yet implemented");
}
}
The #Repeat annotation comes from easytest-core, and the exact definition is here.
When I compile this as java source everything builds (and runs) fine. When I compile the exact same thing as groovy source, I get:
Groovy:Attribute 'times' should have type 'java.lang.Integer'; but found type 'java.lang.Object' in #org.easetech.easytest.annotation.Repeat GroovyTest.groovy
After searching the internets, I found a few similar discussions on SO and jira.codehaus, but those deal with String - GString problems, so the solutions do not work for me.
How can I fix this?
Updates:
java.version=1.7.0_76
groovy.version=2.3.7
Think you're bumping into the fact groovyc doesn't treat final variables as inline constants like javac does
I tried changing your int variable like this:
final Integer value = Integer.valueOf(2).intValue()
which prevents the variable from being treated as an inline constant. After that change I get a compile error from the #Repeat annotation:
Expected Integer.valueOf(2).intValue() to be an inline constant
It looks like there's some acknowledgement of the inconsistency here in a Groovy JIRA: https://issues.apache.org/jira/browse/GROOVY-1628
There's also some further discussion here in this SO thread:
Does it make sense to mark variable as final in groovy?
It doesn't look like you're going to be able to get groovy to match the Java behavior for this scenario.

Unable to get an object from a map giving d/t result for hardcoded value and dynamic value but having equal value

I dont know how to describe the problem, so weird. I have function like this:
long getPersonId(...){
//...
}
The above function returns Id of a person based on some arguments.
So I logged the return value of the function and it is 1.
Then I have code like this:
person = myMap.get(getPersonId(..))
which returns null object but this returns a valid Person object, why?:
person = myMap.get(1)
But as I described before getPersonId(..) returns 1, which basically means
myMap.get(getPersonId(..)) == myMap.get(1)
myMap is typed as Map<Long, Person> myMap
What is happening here?
In Groovy, as in Java, 1 is an int literal, not a long, so
myMap.get(1)
is attempting to look up the key Integer.valueOf(1), whereas
myMap.get(getPersonId(..))
is looking up the key Long.valueOf(getPersonId(...)). You need to make sure that when you populate the map you are definitely using Long keys rather than Integer ones, e.g.
myMap.put(1L, somePerson)
In your original version of this question you were calling the GORM get method on a domain class rather than the java.util.Map.get method, and that should work as required as the GORM method call converts the ID to the appropriate type for you before passing it on to Hibernate.
I am so sorry the problem was when I initialize the map myMap
Map<Long, Person> myMap = [1, new Person()]
when you say something like this the key is an integerbut not a long still groovy not complaining.
So the problem is my method was returning a long value (1L) but my actual key on the map is integer value(1).
So changing my map init to Map<Long, Person> myMap = [1L, new Person()] solved the problem.
Probably this due to dynamic nature groovy but irritating unless you know how dynamic langs behave lol.

Integer getting default value 0 where it required to be NULL in Java [duplicate]

This question already has an answer here:
h:inputText which is bound to Integer property is submitting value 0 instead of null
(1 answer)
Closed 7 years ago.
My requirement looks simple, but I feel I'm doing something wrong in code.
All I need is to insert "null" in Oracle Database when nothing is passed from InputText field of UI.
PaymantSpecFormPage.xhtml
<h:inputText id="startWeek" value="#{flowScope.paymentSpecVO.paymStartWk}" styleClass="inputTypeForm" maxlength="4"/>
PaymentSpecVO.java
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private Integer paymStartWk;
public Integer getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk;
}
}
And in my DB Table
COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null
And when I enter nothing in inputText, then instead of null, 0 is getting entered in Table, which has different meaning in my business logic, please help me to do necessary code changes.
And when I enter nothing in inputText, then instead of null, 0 is getting entered in table
This problem is recognizable as Tomcat's EL parser not taking into account that you're using Integer instead of int. You need to add the following VM argument to Tomcat startup options:
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
h:inputText which is bound to Integer property is submitting value 0 instead of null
Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1
It solved my half of problem, and now its storing null values, but while on retrieve it shows 0 again
This problem is recognizable as JDBC under the covers using ResultSet#getInt() instead of ResultSet#getObject(). This is however essentially a different problem than the first one. You didn't tell anything about how your database access layer is setup, so it's hard to point out a concrete solution.
I don't know how you are reading that xhtml.
At java layer, you can override either the constructor of PaymentSpecVO or the setter for paymStartWk
Try this
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private String paymStartWk;
public String getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk+"";
}
}
But then you might be needing a lot of parsing there.
Edit*
If you enter nothing to your "input Text" the passed value is null but since you're catching it with an Integer, it interprets null to 0.
So when you save it, even after manipulating methods and variables to null , it will store "0" , because basically thats the default value.
Another solution I think is making a custom class that stores Integers
Example :
public class IntegerVal extends Number {
private Max_val = 10000000;
private Min_val = -10000000;
private String default = null;
}
you can do whatever implementations you want ,, like returning null,
You can use that as a substitute to the Integer Class you are using , if you dont wanna use String

Using class constants for [Range] attribute

For a unit test I want to use the Range attribute from NUnit to test inputs to a function in a range. The lower and upper limits of this range are coded into constant properties of a (Singleton pattern) class. I would like to specify the starting point and end point of the Range attribute with the class properties, something like this:
[Test]
public void sometest([Range(MyClass.LOWER_LIMIT,MyClass.UPPER_LIMIT)] int var)
{
//Do something and assertive with the nice variable
}
However, this approach does not work. Although it is not clear from the documentation itself, it seems that the Range attribute must be provided constant variables. While my class constants are static properties with only get defined, this does capture a constant variable.
I posted and answer to this question, but is this really the way to set the range parameters based on class constant in NUnit? Or is there a more elegant solution?
The following example demonstrates how one can use (constant)properties from a class as values used with the Range attribute from NUnit.
const int LO_LIM = 1;
const int HI_LIM = 10;
[Test]
public void assertConstantsCorrect()
{
//Will fail if constants change during development!
Assert.AreEqual(MyClass.LOWER_LIMIT,LO_LIM);
Assert.AreEqual(MyClass.UPPER_LIMIT,HI_LIM);
}
[Test]
public void sometest([Range(LO_LIM,HI_LIM)] int var)
{
//Do test
}
The first step is to define constants in your test class, as the Range attribute only works with constants. These constants take the same values as the constants defined in the properties of your class.
Second is a Test created to verify that they correspond. If at a later date and time the constants in MyClass change, the failure in this test will notify you of this change. Take note that if this test does not pass, any other test using those constants can be regarded as invalid as they rely on false asumptions!
Lasty are your actual tests that use those values in the [Range( start, end)] clause.
Alternatively, you can also make use the [TestFixtureSetUp] attribute instead of the [Test] attribute for the assertConstantsCorrect() method to make all tests in the fixture fail in case the assertConstantsCorrect() fails.
Yet another alternative is to make a custom attribute to work for specific methods you as programmer annotate and make those methods fail when assertConstantsCorrect() fails.

Resources