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.
Related
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 7 days ago.
Improve this question
I have a method that uses a static method defined in a Utils.java. The questions is, is it ok to test a method that uses a static method and how should I do that? I saw that some people uses PowerMockito but from what I understood, that is a code smell.
It is definately okay to test static method, but the thing is
You have to mock the dependent functions,
#Test
void testStaticMethod() {
MockedStatic<YOURSTATIC_CLASS> utilsMocked = Mockito.mockStatic(YOURSTATIC_CLASS.class)
utilsMocked.when(YOURSTATIC_CLASS::name).thenReturn("yourwantedassertable");
assertThat(YOURSTATIC_CLASS.name()).isEqualTo("yourwantedassertable");
}
Yes, it is ok. I advise you to watch this :
https://www.baeldung.com/mockito-mock-static-methods
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 3 years ago.
Improve this question
I have a method doing something like this
class CodeProvider implements Provider {
List<String> getCodes() {
return List.of("1", "2", "3");
}
}
Is there any reason to write a unit test for it like this?
#Test
public void test_GetCodes() {
assertThat(getCodes(), is(of("1", "2", "3")));
}
In order to have more context. This class is part of Provider hierarchy, that doesn't have any more logic than this. Providers only reason to exists is to be used as parameter for an common external service call.
As the commenters have pointed out, testing that the data matches exactly doesn't seem that useful. However, there are other types of tests you can write for such a method.
(Taking into account that this is a simplified sample and that the underlying data may be more complex:) With source code like this your method becomes an abstraction for the underlying data. Maybe your client code will rely on some properties of this data, for example that the data items are ordered, or that there is an upper bound on their cardinality, etc. In cases where getCodes is likely to evolve, unit tests will help gain confidence that your properties/assumptions of interest do not become violated as as the code changes.
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 1 year ago.
Improve this question
Where is preferred place to store functions for improve readibility of streams?
For example:
private BinaryOperator<Cash> sumPayments() {
return (payment1, payment2) -> payment1.sum(payment2);
}
I'm not sure if it should be in service class where it's used, but maybe it will be in many places so whats about some extra static Utils? or in entity?
Where possible avoid unnecessary helper methods.
In this particular example, the helper method can easily be replaced with a method reference, which is (arguably more) readable:
Cash total = subtotals.stream().reduce(Cash.ZERO, Cash::sum);
In other cases, where you actually do need a helper: think about where you would search for such a helper method in a year, when you have to adapt your now-barely-familiar code to new requirements.
Is it more entity related? Put it in the entity class
Is it more business logic related? Put it into the business logic
Is it generally useful? Put it into a utility class
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.
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