M
M
Matvey Slavenko2015-10-14 21:08:51
Java
Matvey Slavenko, 2015-10-14 21:08:51

How to gracefully populate static data when unit testing in Java, JUnit?

Faced the following problem. I'm writing unit tests for a Java project (personal). And very, very often you have to write such canvases of code (for ease of perception, I reduced the number of objects):

LinkedList<Vertex[]> sharedData = new LinkedList<>();

sharedData.add(
                new Vertex[]{
                    new Vertex(0, 0),
                    new Vertex(10, 0),
                    new Vertex(0, 10)
                });

Something tells me that I'm doing something wrong, and for sure it can be done easier, more beautiful and elegant. Ideally, I would like to be able to write using a "fluid interface". Well, something like this:
LinkedList<Vertex> sharedData = listFiller.Vertex().add(0, 0).add(10, 0).add(0, 10).toList();

If I worked only with Vertex, then I would write some helper class and calm down. But the fact is that I need this for completely different types. This is the aforementioned Vertex, and Section, which is in theory constructed from two points (i.e., de facto, four numbers), and Vector (everything is better here, it is always on a plane, i.e. two numbers to create an object ).
I will be very grateful for your help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question