How to manage testing?

I read about TDD and I tried it in my new project.

TDD cycle

I understand that in TDD this is similar to Blackbox testing, i.e. what matters , not how . So, I came to the conclusion and stopped testing private methods, having read about it on many posts, since this is incorrect.

However, I could not do this for these reasons.

I will show you an example: I have a program that reads a text paragraph, so I wrote something like this in my test method (for tdd step1).

/* My program reads a textual paragraph from file and saves it to my custom paragraph object; */ 

So I made this method to create a RED case.

 public void paragraphMustNotBeNullTest(){ File f = new File("some path") ParagraphReader reader= new ParagraphReader(); reader.read(); assertNotNull("my custom paragraph is null",reader.getCustomParagraph()); } 

I wrote the following code:

 package com.olabs.reader; import java.io.FileInputStream; import java.io.InputStream; import com.olabs.models.OlabsParagraph; public class Para { private String paragraphText = null; private Paragraph oParagraph = null; public Paragraph getCustomParagraph() { return oParagraph; } public void setoParagraph(Paragraph oParagraph) { this.oParagraph = oParagraph; } public void read() { InputStream is = new FileInputStream("abc......"); // .. String text = is.read(); // assume I got text read from file. this.paragraphText = text; } private void createCustomParagraph() { Paragraph p = new Paragraph(); p.setText(paragraphText); p.setId(1); p.setType("Story"); ........... } private void countWords() { // counting words in paragraph logic } } 

Now the problem is that I know in advance that I will use countingwords and createCustomParagraph as private methods.

So, in such cases, I should:

  • creating them as public and following tdd loops.

  • make them confidential.

  • remove tests for them (since methods are now private and inaccessible for tests). I think this is a rather cumbersome and incorrect way to make tdd.

I am confused by this. Everyone says that writing code only after you write a failed test, but here, if I know that I will write a private method , how will I do it?

I ask you to correct me if I am mistaken somewhere. Also, if possible, give some real example ...

In addition, I am afraid that most of the time I will edit tests or delete them due to problems with access or refactoring specifications ...

Note. This is not a duplicate question. I do not have a good answer for real-time situations. In all the examples I've seen, they show only one class with default or public specifiers, so they really don't show how to work in real time.

+5
source share
3 answers

From my personal experience, you are testing the class and interface for this class with the rest of your application. Your TDD test cases should be written against the public methods of your class. The point is to verify that when stimulating the class displays the desired results for passing your test criteria. Internal private methods will be tested as part of testing the public interface, but the goal is to check if the public interface is working. You say that you know in advance that private methods will be used, but if someone reorganizes how things are done in the class without changing the external behavior, you can still use existing tests and make sure that refactoring does not violate the previous working method.

+4
source

If the method is a detail of the implementation of the main thing that you are building, which should remain closed, this does not mean that there is no way to write a test for it. You can make this functional part of another class where it can be publicly available, write tests that run it directly, and then call it confidentially from the main thing. This way you can test private methods, no tests will be deleted, and you won’t have to go through distortions, checking something indirectly when the thing used may not provide access to everything that you want to check. And now, instead of having private methods that are hidden bits of gnarly code, you have well-tested building blocks ready to be reused elsewhere.

+1
source

Very good question. I also asked many times. And overall, the answers said that you need to think about unit tests that go before implementing basic functions like frames . This framework strictly defines functionality.

Thus, you may not even know about private methods before you intend to implement some specific classes. But since you have a RED test suite, you need to make it green. It doesn't matter how you do it (public or private methods).

Goal number one is to write the minimum lines of code that should be covered with unit tests. I covered the topic of TDD in more detail on my blog.

0
source

All Articles