Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to write a test case for the given method
private String productCode;
public PaymentManager() {
this.productCode = "SMART-TV;
}
#Override
public boolean isResponsibleFor(TransactionDetailResource resource) {
return productCode.equals(resource.getProductCode());
}
How to write test case for this method
First, create a test class and a test function
#Test
public void testIsResponsibleFor() {
...
}
(If you are using Eclipse, you can press Ctrl+J, It will automatically create the test class for you)
Then in your test function you must create your two objects, one PaymentManager and one TransactionDetailResource.
PaymentManager p = PaymentManager();
TransactionDetailResource t = TransactionDetailResource();
t.setProductCode("SMART-TV")
I have assumed you got a Setter in your TransactionDetailResource class.
AssertThat(p.isResponsibleFor(t),is(Boolean.True))
This is a partial answer but It can gives you a starting point
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a question about param, what param to present the class, such as if I findBuildingsType( buildingEntities, Village) it will return all village building as a result
Welcome.
You can deliver a Class via arguments like this:
private List<Building> findBuildingsType(List<Building> buildings, Class<? extends Building> typ) {
List<Building> re = new ArrayList<>();
for (Building building : buildings) {
if (typ.isInstance(building)) {
re.add(building);
}
}
return re;
}
Especially for your problem consider using streams:
List<Building> buildingsOfTypeMansion = buildings.stream().filter(building -> building instanceof Mansion).collect(Collectors.toList());
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am just learning Java.
And I want to improve my code and answer the question below.
3.Inside the main() method of Simulator , create an instance of a "Cat" object, and invoke that object's run() method.
Does my code require additional information? I want to answer the above question and this is what I have so far:
class Simulator {
public static void main(String[] args) {
Cat c = new Cat();
System.out.println(c);
}
}
Am I correct? I found the second class returns as an random integer. Will that work to invoke the run method.
There are so many things wrong...
"Code snippets" are for Javascript. Your program is Java. Java <> Javascript :(
Your "Cat" object implements a thread. You use threads for "concurrency", to do things "in parallel". Does your "meow" really merit spawning off a thread?
What about member "1"? You declare it. You initialize it. And then you fail to use it for ANYTHING. Q: Why bother?
System.out.println(c) prints the "object"; it doesn't print anything "meaningful".
Suggested modifications:
public class Cat
{
private int i;
public void meow() {
System.out.println("Meowing: " + i);
}
public Cat(int i) {
this.i = i;
}
public static void main(String args[]) {
Cat cat = new Cat (1);
cat.meow();
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
From immutability's point of view, is there any concerns for this code ?
public class mainPkg {
private boolean MyFunc1(MyInfo info) {
List<MyObj> myList = new ArrayList<MyObj>();
anotherPkg.MyFunc2(info, myList);
anotherPkg.MyFunc3(info, myList);
return CollectionUtils.isEmpty(myList);
}
}
public class anotherPkg {
public static boolean MyFunc2(MyInfo info, List<MyObj> myList) {
if(info.version < 2) {
myList.add(new MyObj('wrong version'));
return false;
}
return true;
}
}
MyFunc1(), MyFunc2() are used only in 1 place, and will only be used in this place. When i pass list as argument into myFunc2, it's like the classic call by reference. Not using global variable for myFunc1().
From immutability's point of view, what're are the concerns for this code ?
You are mutating the list referenced by myList in MyFunc2, therefore, your code is not immutable, therefore, it doesn't make sense to look at concerns "from immutability's point of view".
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I don't find the appropriate solution for my question. As far as I know returning null is a not a good way to write clean code, as the book Clean Code says. However there are many different opinions about this practice, and I am not sure about which one fits on my function.
private EArea getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : exsitingAreas) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return existingArea;
}
}
}
return null;
}
What should I return if there is not an existing similar area?
PD: For optimize my code I am breaking the loops with the return if an existing area is founded.
You should take a look at the Optional class!
Make your method return type Optional<EArea> and simply return Optional.ofNullable(existingArea) you will have to slightly modify your Code but the benefits of Optional are really worth it!
Finally I used Optional Class to solve my problem.
Here is the code:
private Optional<EArea> getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : baseLineService.getBaseLine().getAreas()) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
for (EPoint importedAreaPoint : importedAreaPoints) {
for (EPoint existingAreaPoint : existingAreaPoints) {
if (importedAreaPoint.equals(existingAreaPoint))
return Optional.of(existingArea);
}
}
}
return Optional.empty();
}
And here is how I check the returned value:
if (getSimilarExistingArea(importedArea).isPresent())
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public void printSummaryForPatient(String name){
Patient p = findPatient(name);
p.printPatientSummary();
p.computeBMI();
}
My method to test:
#Test
public void testPrintSummaryForPatient() {
Patient patient_adult=new Patient("Ted",24,1.90,70.0,"Leicester");
//Patient Patient_child=new Patient("Kate",4,1.90,70.0,"Leicester");
// Patient Patient_elderly=new Patient("Bill",124,1.90,70.0,"Leicester");
surgery_N.findPatient("Ted");
patient_adult.printPatientSummary();
assertEquals("Ted", patient_adult.getName());
assertEquals("-----------PATIENT SUMMARY: ---------"+"\n"+"NAME: "+patient_adult.name+"\n"+"Age: "+patient_adult.getAge()+"\n"+"Address: "+patient_adult.getAddress()+"\n"+"Height: "+patient_adult.getHeight()+"\n"+"Weight: "+patient_adult.getWeight()+"\n"+"------------------------------"+separator,ans.toString());
patient_adult.computeBMI();
assertEquals(19.390581717451525, patient_adult.computeBMI(), 0.0);
}`
The problem is that the way I use to test doesn't cover the original file at all. Hope I can get some help from you guys.
You could assign a different writer to System.out (assuming that's where your output goes) and inspect what gets written there. In general, you probably want to make the writer a parameter of printSummary or inject it into the class somehow.
So basically you want to do this:
#Test
public void testPrintSummaryForPatient() {
Patient patient_adult=new Patient("Ted",24,1.90,70.0,"Leicester");
surgery_N.printSummaryForPatient("Ted");
}
But can't do any asserts, because the Patient is not returned.
Do you want to return the patient?:
public Patient printSummaryForPatient(String name){
Patient p = findPatient(name);
p.printPatientSummary();
p.computeBMI();
return p;
}
After that you could use your assertions. It seems more like a conceptual problem of how you organize your methods.
You have methods in printSummaryForPatient, that don't seem to do anything. Their return value is not returned or saved.