JaCoCo add missing coverage [duplicate] - java

This question already has answers here:
Code coverage does not reach class declaration
(2 answers)
Jacoco is not able to cover a class containing only static methods
(1 answer)
Closed last month.
I'm trying to achieve 100% coverage but SonarQube/JaCoCo says line 7 is not tested.
I did this test to test the main:
try ( MockedStatic<SpringApplication> springApplicationMockedStatic = Mockito.mockStatic( SpringApplication.class ) )
{
ApiApplication.main( new String[] {} );
springApplicationMockedStatic.verify( () -> SpringApplication.run( ApiApplication.class, new String[] {} ) );
}
How can I fix the missing coverage?
Thanks

Related

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

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.

How to fix missing F on Java [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 8 months ago.
I try to make a game Flappy Bird and want to test my result on DesktopLauncher, but something is happening that makes two errors. I can't find any F on the list.
package com.mygdx.flappy;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
public class DesktopLauncher {
public static void main (String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.width = 480; //this is the error part
config.heigth = 800; //this is the error part
new Lwjgl3Application(new MainFlappyBird(), config);
}
}
here's the picture
Config is an object of type „Lwjgl3ApplicationConfiguration“ And this type does not have properties „width“ and „height“. That‘s why you can‘t assign values to this property, because it does not exist.

Java strange issue changing string [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I've tried searching for this, but then I'm not sure when how to describe it.
I have a method that formats some data from a hashmap to go into a mySQL table:
private String valuesList() {
String valuesList = "";
HashMap<String,String> data = getData();
for(Map.Entry<String, String> entry : data.entrySet()) {
String value=entry.getValue();
valuesList+="'"+value+"',";
}
valuesList = valuesList.substring(0, valuesList.length() - 1);
return valuesList;
}
Most of the time that works fine, but in some cases one of the values has an apostrophe in, which leads to an output like this:
'4577314','18-02-2017','null','4566974','null','Overseas Domestic Workers' Rights Bill','1124','null'
Note the 'Overseas Domestic Workers' Rights Bill' bit at the end. I thought that would be easy to fix by changing
valuesList+="'"+entry.getValue()+"',";
to
valuesList+="'"+entry.getValue().replace("'","")+"',";
but the method now throws a null pointer exception at that line. In fact any kind of change to that string such as .trim() does the same, throwing a null.
I'm completely stumped now
You can escape quotes from value like this
value = value.replaceAll("'","''");

Java Optional "while not null" equivalent [duplicate]

This question already has answers here:
do-while with Java8-Optional
(5 answers)
Closed 5 years ago.
Let us assume we start out with this code
while ((element = getNextElement()) != null) {
// do something
}
and want to rewrite it so that getNextElement() returns an Optional as opposed to something that might be null.
What would be the preferred way to do that? Something like
while ((element = getNextElement()) && element.isPresent()) {
}
... or is there a better way?
There is also
for (Optional<ElementType> element = getNextElement(); element.isPresent(); element = getNextElement()) {
// do something
}
Are there other/better options (pun intended, I suppose) for this?
Stream.generate(this::getNextElement) ...
The above would generate a Stream<Optional<Element>>. I believe that in java 9 there is a flatMap possibility.
For now:
Stream.generate(this::getNextElement)
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(...);

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