For loop Testing in java [closed] - java

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 7 years ago.
Improve this question
public void numberOfChoices() {
for (int i = 0; i < 10; i++) {
int guess = scan.nextInt();
}
}
how can i test this for loop in junit. my method should be void.
i think i couldn't make myself clear. my question is that how can i test the loop that actually it goes ten time? i hope you understand now. but i don't know some people just try to be oversmart and give negative point to my question i think if you couldn't understand ask for more explanation. and this is question is not that simple to solve too so don't understand why i got negative....

Writing code easily to test is the first step for writing test.
Every single method should have their meaning and only do one thing.
If you face problem in writing test, you might think about it.

If your method must be void and you don't work with database I would advise to create getters for the variables that you want to verify. For the loop you might want to have some Array or List, for example, and then in your test do something like this:
assertEquals(expectedList, yourClass.getList);
However, it is not the ideal scenario because you should not change the production code to fit your testing needs. If you work with database then create a separate query in the test to fetch the needed data.

Related

Does it make sense to create tests like this? [closed]

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 4 years ago.
Improve this question
lately I'm coming deeper into unit tests and I kind of stuck.
Example. we have method like this:
boolean isCheckOutChecked(NmCommandBean clientData) {
return "checkOut".equalsIgnoreCase(clientData.getTextParameter("checkOut"));
}
Most of it depends by what clientData returns. There are two possibilities:
return null if can not find text parameter
return string value if did
Well... I have created two tests but wondering if does it have any sense to have tests like this? What will you do in this scenario?
#Test
void shouldReturnTrueWhenCheckoutIsChecked() {
doReturn("checkOut").when(clientData).getTextParameter("checkOut");
boolean checkOutChecked = formProcessor.isCheckOutChecked(clientData);
assertTrue(checkOutChecked);
}
#Test
void shouldReturnFalseWhenCheckoutIsNotChecked() {
doReturn(null).when(clientData).getTextParameter("checkOut");
boolean checkOutChecked = formProcessor.isCheckOutChecked(clientData);
assertFalse(checkOutChecked);
}
Since you have hard-coded checkOut I'd say yes, we have these kind of tests in our product - called "safety net" tests, in case someone changes on UI side that parameter, we want to fail in tests, not in real code.
In places where there are conditions/branches, it is very useful to have unit tests, so that all the basic scenarios are covered in unit test cases itself. This reduces the feedback time (the application need not be deployed to test the condition) and helps in faster development.

test a java method in netbeans [closed]

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 6 years ago.
Improve this question
Let's say I found this piece of code and I want to test it in Netbeans before I incorporate it:
ArrayList list;
for(String s: list)
{
Integer c = stringsCount.get(s);
if(c == null) c = new Integer(0);
c++;
stringsCount.put(s,c);
}
Is there a way to test the above code in Netbeans without having to create a temp class with a main? I believe there is a way in Eclipse but I am looking for a way in Netbeans. Thank you.
Is there a way to test the above code in Netbeans without having to create a temp class with a main?
As far as I know, you can't do that. Your program needs an entry point. The main method is supposed to be your program's entry point.
If you are reluctant to always create a new class. You can always have a class for testing. Just replace the old codes with new testing codes whenever you need it.
P.S: I don't think it can be done in Eclipse as well.

What is the meaning of this code in Java? [closed]

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 7 years ago.
Improve this question
I was going through some of the questions online and I came across this code. I couldn't understand the meaning of this particular line of code. Please can anyone explain me the meaning of this line of code.
So, there are two classes Student class and Course class. Each class has its own setters and getters. Now, there is this one output statement which is something like this:
System.out.println("I like " + student2.getCourse().getCourseCode());
**bdw student2 is an object of the Studentclass
The part that i am confused is student2.getCourse().getCourseCode()
It looks like two methods are somehow linked or something. What is the meaning of this line. Any article or suggestions is really appreiciated. Thank you!
student2.getCourse() returns a course. It is possible to call getCourseCode() on a course. This is an example of method chaining.
The equivalent code is:
Course tempVar = student2.getCourse();
System.out.println("I like" + tempVar.getCourseCode());
As you learn more programming, you will find this to be consistent. A variable declared Course is clearly a course, but the result of a method called Course.getCourse also is a course, and you can use it as a course (including calling the methods of course (no pun intended)).
System.out.println("I like " + student2.getCourse().getCourseCode());
I've formatting the line above fro reference. Stundent2.getCourse() returns the course that the student is enrolled in (presumably). getCourseCode() is then called on the course that was returned previously

Refactor code java [closed]

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
I have the below piece of code and statements 2(scopeId) and 3(productScopeId) are repetiting in many methods. How can I refactor the below methos in such a way that it can be used in many methods. I don't want to create a class with these two variables and return scope and product scope id. Can you please suggest any other better way. Thanks in advance for your help.
public List<Product> getAllHydroProducts()
throws RepositoryException {
String productTypeId = getProductTypeId(Group.GROUP_TYPE_NAME_HYDRO, getAllProductTypes());
String scopeId = getScopeTypeId(Group.SCOPE_TYPE_NAME_HTX, getAvailableScopes());
String productScopeId = getScopeTypeId(Group.SCOPE_TYPE_NAME_PROJECT, getAvailableScopes());
return getProductsByTypeAndScope(productTypeId , scopeId ,productScopeId );
}
I work under the assumption that you have several constants like Group.GROUP_TYPE_NAME_HYDRO, and all your methods are just like the one you show us, but with different constants (I believe you should add those other methods to your question, since without them there is no visible repeated code).
You have two possible scenarios (again, with more code would be easier):
1) You are only changing the product type constant, keeping the other two all the same in your other methods. If that's the case, you can just extract a more generic method like:
public List<Product> getAllProductsOfType(YourConstantType productType) throws RepositoryException {
return getProductsByTypeAndScope(
getProductTypeId(productType, getAllProductTypes()),
getScopeTypeId(Group.SCOPE_TYPE_NAME_HTX, getAvailableScopes()) ,
getScopeTypeId(Group.SCOPE_TYPE_NAME_PROJECT, getAvailableScopes())
);
}
Note that I don't know what your constants are, since you did't put them on your question, that's why YourConstantType is there.
Then your existing methods would just call this new one with their parameter.
2) Another scenario: You also have different scopes on your methods. In this case, I might go with a builder pattern
Also... is that a checked exception what I see there? I hope not ...

What is a JUnit test case, and how would I go about writing one? [closed]

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 9 years ago.
Improve this question
In class, we learned about test cases in Java. The professor didn't go over them that well and all the sources I found online weren't helpful as much. I understand that they help to catch any bugs in the porgram, but how does it work? In the example in class, the professor had something like:
#Test
public void test1(){
Webpage n = new Webpage("www.facebook.com");
asssertequals(n.getURL(), "www.facebook.com");
}
Is it okay for getURL and Webpage to be used even though the actual code it hasn't been written yet (she wants us to always write our test cases before we code)? When writing a test case, do we write it as if we have written the whole code before, and test whatever values we think will pass or fail? I have alot of questions, I know.
Also, just to be clear on my understanding, the variable n is set equal to whatever Webpage has between its parentheses, but the new operator creates the object that is supposed to be assigned to n.
Ok so to start of Test cases are especially good and important for larger projects. As this is automated testing it saves developer sh*t loads of time ;).
#Test
public void test1(){
Webpage n = new Webpage("www.facebook.com");
asssertequals(n.getURL(), "www.facebook.com");
}
So first is first:
#Test
this annotations is used so the compiler knows which methods are actual tests.
Webpage n = new Webpage("www.facebook.com");
In here you are simply declaring variable "n" to be url (facebook)
asssertequals(n.getURL(), "www.facebook.com");
This is where the magic happens. assertequals checks if the variable equals to what we expect. If they are equal in this case test passes. but if you had this
Webpage n = new Webpage("www.facebook.co.uk");
asssertequals(n.getURL(), "www.facebook.com");
Then the test will simply fail because expect www.facebook.com but we give co.uk
There are also plenty of other asserts you can use for testing
Also for more info refer to this website: i have found it particularly well organized and written as well as easy to understand
http://www.tutorialspoint.com/junit/junit_overview.htm

Categories