How to capture the error message in private function [duplicate] - java

This question already has answers here:
JUnit test for System.out.println()
(14 answers)
Closed 5 months ago.
I have a function compare which called private function compareRuleRanks, and println the error or correct message when condition met, how can I capture the message when I do unit test? I try to use AssertionError but didn't work, how can I do that?
AssertionError(dataCompare.compare("Rule1", "Rule2"));
public void compare(){
compareRuleRanks(rule1, rule2);
}
private void compareRuleRanks(rule1, rule2) {
if(rule1.rank != rule2.rank) {
println("The ranks are not in order");
}
println("rules are same");
}

When unit-testing compareRulesRanks, you can definitely call the function but you would have to measure whether the intended reaction was triggered.
That means you would have to check whether println() got called as expected.
If you cannot do that you probably have to improve your code for testability.

Related

How to mock the function in a loop using Mockito in Java to return different value for each iteration [duplicate]

This question already has answers here:
How to tell a Mockito mock object to return something different the next time it is called?
(5 answers)
Closed 2 years ago.
I am new to using Mockito. In my logic I need to mock a function that is inside the loop and for every iteration, it should return different value.
Example :
for(value : values )
{
int i = getValue(value);
i=i+1;
}
if(i=somevalue)
{
some code
}
else
{
Some other code
}
So if I mock getValue() method to return a particular value. Everytime, it is returning the same value and only one part of if else is covered.
Can you please suggest me a way such that everytime in the loop getValue() is returned different value.
Thank you !
Since you have an input in getValue() you can use that.
when(mockFoo.getValue(value1).thenReturn(1);
when(mockFoo.getValue(value2).thenReturn(2);
when(mockFoo.getValue(value2).thenReturn(3);
But if you just don't care you can return different values in a sequence.
when(mockFoo.getValue(any()))
.thenReturn(0)
.thenReturn(1)
.thenReturn(-1); //any subsequent call will return -1
// Or a bit shorter with varargs:
when(mockFoo.getValue())
.thenReturn(0, 1, -1); //any subsequent call will return -1
Also please note, that if(i=somevalue) will always be true, you might want to use if (i == somevalue).

how do i test instances in junit? [duplicate]

This question already has answers here:
How to write a Unit Test?
(5 answers)
Closed 4 years ago.
Below is the code I want to test
public class EPATestMode {
public static final int INVALID_MPG = -1;
private int odometerAtReset;
public EPATestMode() {
odometerAtReset = 0;
}
public void resetReadings(int milesDriven) {
// Use the current mileage as the new baseline
odometerAtReset = milesDriven;
}
public double mpg(int currentOdometer, int gallonsBurnt) {
if (gallonsBurnt == 0) {
return INVALID_MPG;
} else {
int milesDriven = currentOdometer - odometerAtReset;
return milesDriven / gallonsBurnt;
}
}
}
This is my first testcase I want to do , on the instance variable INvalid MPG but when I do this , there is a line crossing out "assertEquals". Very confused about this.(Also new to JUnit testing)
#Test
public void testInvalidMpg() {
EPATestMode MpgTest = new EPATestMode();
double results=MpgTest.INVALID_MPG;
assertEquals(results,-1)
}
You don't state your environment, but most likely it behaves similar to Eclipse (maybe it is Eclipse?) in that a line through the code is an indication that you're using a deprecated API. In this case, if you go to the Junit API documentation, you'll see that assertEquals for doubles like you are calling is deprecated in favor of one that includes a delta. API
The idea is that floating point numbers are inherently inexact, and so comparing them is inexact at best. You need to also include a delta so that you can indicate how far apart the numbers can be and still be acceptable.
So basically you want:
assertEquals(results, -1, .000001); // For example.
On a side note, I understand that you're just trying to wrap your head around this - and consequently you're probably trying to come up with a simple test just to get "something working". But tests like that - where you compare a class constant to see if it's what you input - aren't particularly useful. I would be more inclined to test to make sure that supplying 0 as your "gallons burnt" returns the proper "INVALID_MPG" constant.
Line crossing out means the method is deprecated. http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals(double, double)
The new method to use is below
public static void assertEquals(double expected,
double actual,
double delta)
The delta is how much difference the actual and expected can have.

Cucumber, java - How can I use return value in the next step? [duplicate]

This question already has answers here:
Cucumber Java - How to use returned String from a step in next step?
(2 answers)
Closed 4 years ago.
I can't use returned value in the next steps.
Exaple:
I have a method:
public class learn_BDD {
#Test
#When("^ I have \"([^\"]*)\" dolars on acount$")
public String checkAcount(String amount){
String Value = Acount.checkValueOfAcount();
return returnValue;
}
#Test
#Then("^Check how much I spend$")
public void howMuchISpend(String returnValue){
String actualValue = Acount.actualAcountState();
if (actualValue < returnValue) {
System.out.println("You are spend money");
}
}
In this case I give than error:
***BDD is declared with 1 parameters. However, the gherkin step has 0 arguments [].***
If somebody can help me I will be grateful.
The error you are seeing is due to a lack of a capture group in the regular expression provided to the Then annotation.
The howMuchISpend method has 1 argument so Cucumber expects 1 capture group, for example, #Then("^Check how much (.+) I spend$"). The string captured by (.+) is passed as the value of its argument.
#Test
#Then("^Check how much (.+) I spend$")
public void howMuchISpend(String returnValue){
String actualValue = Acount.actualAcountState();
if (actualValue < returnValue) {
System.out.println("You are spend money");
}
}
Regarding the other part of your question, each step definition is independent, the value returned by a test method is ignored. If you want to pass values from one step to another you have to use a class scoped variable.
BTW, making steps dependent should be avoided.

Why does a print statement not execute when method called from "empty" main [duplicate]

This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 5 years ago.
(I really don't know how to phrase the question any better. I'm sure there is a concept involved here that I'm unaware of, so please suggest a better phrasing if you can--or direct me to an answered question if this turns out to be a duplicate)
I've been playing around in Java and found some behavior I can't explain. In the following code, I'd expect a 0 to be printed. However, nothing is printed. The only possible explanation I can come up with is that the main method ends before the printstream is flushed, but it doesn't make sense to me why that might be. In short, whats up with this code printing nothing instead of 0?
class Test {
public static void main (String [] args) {
if(false && method()){
}
}
public static boolean method(){
System.out.println(0);
return true;
}
}
Because the method is not called. The false causes the and to short-ciruit.
if(false & method()){ // <-- body will not execute, but the evaluation
// does not short circuit.
or
if(false || method()){ // <-- body will execute, method() is true
or
if(method() && false){ // <-- body will not execute, because of false.
will work as you expected.

Java - metode inside a metode syntax issues [duplicate]

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 5 years ago.
Really don't know what it's called so I'm having a hard time searching for the answer.
Anyhow, I want to make a metode with metode inside (if that's even possible?).
public void log() {
public makeLogElement() {
//making a logelement to write inn
}
public write(String text) {
logelement.setText(logelement.getText() + text);
}
}
log myLog = new log();
myLog.makeLogElement();
myLog.write("This'll be written in the log");
What is the right syntax for making something like this?
It's not possible. But you can create a class inside a method.

Categories