Answer the question
In order to leave comments, you need to log in
What are JUnit tests and what are they used for?
hello everyone, this is my first time writing here, so don’t kick too much =)
in general, the problem is this, I don’t understand how JUnit tests are carried out with sorting algorithms (and are they carried out with sorting at all?)
On the Internet, there is a banal example of a unit test of a method addition, yes, everything is clear with this.
Based on this addition example, I made a sorting unit test in which I first entered the elements into the array, then did the sorting
and
@Test
public void testInsert() {
ArrayIns arrayIns = new ArrayIns(10);
arrayIns.insert(5);
arrayIns.insert(7);
assertEquals(5,arrayIns.getFirst()); // сравнение ожидаемого результата с возвращаемым значением
Answer the question
In order to leave comments, you need to log in
Just an instructive example.
You need to test the class. What does it mean? Test its behavior, at least the basic one. Those. preferably all methods that can be used. Like a black box. (we don’t test hidden ones and you don’t have to test getter&setter).
How to do it?
You have added one getFurst() method. Probably for testing. But the first elements should not be limited to checking. Therefore, the getItem(int) method is needed rather; This leads to a change in the interface of your class (replacing an unnecessary method with a necessary one)
We made changes, tested it. OK.
Further write the test on insert. Some. And at the end you get an exception. The index is larger than the size of the array. Opa. You must either explicitly describe this case in the documentation (throw an exception), or do something else. Point out to the user this slippery case.
Further. There is a display method
How to test it automatically? No way. This suggests "something is wrong". Not so, rather because this method should not be in this class. class is sorting. The output to the screen should be handled by another class. If you suddenly need to change the output, then you have to pick a class that is already working and tested. And you will paw all the code again, at the risk of pulling something that will covertly change the correct operation.
Display to another class.
so. removed getFirst. display. added getItem.
the class is well tested. and a more user-friendly interface.
Those. takeaway from everything: Testability of a class depends on a good interface. Requires it.
A testable class is almost always (most often) a class that will be easy to use. A class that is difficult to test will most often be difficult to use.
More or less like this.
Specifically on the topic.
Just by the test code, no questions. Question about class testing.
Do you want to check if sorting works correctly only through the first number in the array? What if the sort result is "1 10 5 7 2 5", "1 5 7 10 2" or even "1 2 5 10 7"? Is this all true?
Your unit test writing is good. Checking the sorting condition is not correct.
below is a link to the source, how can I implement a JUnit test for it, if you understand, don’t pass by pliz =)
help the noob figure it out, I understand what the guys above are talking about, that I check only the first value, but I need to check the entire array, but how to implement it I don’t know about the unit test =)
PS I hope there is a guru who will give an answer or point where to look or what to read
https://github.com/leVenTel8/SortirovkaVstavkoi/bl...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question