Implementing the Builder pattern (fourth part) + Junit tests. Links to previous implementations of Builder-a: Part 1 http://clck.ru/8pke3 Part 2 http://clck.ru/8pkeF Part 3 http://clck.ru/8pknj

1) Abstract BaseCarBuilder class.

public abstract class BaseCarBuilder { public abstract void buildBody(); public abstract void buildColor(); } 

2) Car class.

 public class Car { public enum Color {RED, BLACK, WHITE} public enum Body {SEDAN, CABRIOLET, LIMOUSINE} private Body body; private Color color; private String name; public Car(String name) { this.name = name; } public String getName() { return name; } public Body getBody() { return body; } public void setBody(Body body) { this.body = body; } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } @Override public String toString() { return "Car{" + "body=" + body + ", color=" + color + ", name='" + name + '\'' + '}'; } } 

3) CarBuilderDirector class.

 public class CarBuilderDirector { private final BaseCarBuilder baseCarBuilder; public CarBuilderDirector(BaseCarBuilder baseCarBuilder) { this.baseCarBuilder = baseCarBuilder; } public void construct() { baseCarBuilder.buildBody(); baseCarBuilder.buildColor(); } } 

4) The VolvoBuilder class which inherits from BaseCarBuilder.

 public class VolvoBuilder extends BaseCarBuilder { private Car car; public VolvoBuilder() { car = new Car("Volvo"); defaultVolvoBuilder(); } public Car getCar() { return car; } @Override public void buildBody() { car.setBody(Car.Body.LIMOUSINE); } @Override public void buildColor() { car.setColor(Car.Color.BLACK); } private void defaultVolvoBuilder() { car.setColor(Car.Color.WHITE); car.setBody(Car.Body.SEDAN); } } 

5) Client class

 public class Client { public static void main(String[] args) { CarBuilderDirector director = null; VolvoBuilder volvoBuilder = new VolvoBuilder(); director = new CarBuilderDirector(volvoBuilder); director.construct(); System.err.println(volvoBuilder.getCar().toString()); } } 

Now the implementation of Junit tests. It was necessary to implement the following tests. These are my second junit tests.

alt text

Test number 1

 public class CarTest { @Test public void testWhiteSedanCarToString() { Car car = new Car("My Car"); car.setColor(Car.Color.WHITE); car.setBody(Car.Body.SEDAN); final String actual = car.toString(); final String expected = "Car{body=SEDAN, color=WHITE, name='My Car'}"; assertEquals(expected, actual); } } 

Test number 2 and 4

 public class VolvoBuilderTest { // ั‚ะตัั‚ โ„– 2 @Test public void testCarBlackLimousine() { VolvoBuilder volvoBuilder = new VolvoBuilder(); volvoBuilder.buildColor(); volvoBuilder.buildBody(); String actualBody = String.valueOf(volvoBuilder.getCar().getBody()); String actualColor = String.valueOf(volvoBuilder.getCar().getColor()); assertEquals("BLACK", actualColor); assertEquals("LIMOUSINE", actualBody); } // ั‚ะตัั‚ โ„– 4 @Test public void testWithoutBuildBodyAndWithoutBuildColor() { VolvoBuilder volvoBuilder = new VolvoBuilder(); final String expectedCar = "Car{body=SEDAN, color=WHITE, name='Volvo'}"; final String actualCar = volvoBuilder.getCar().toString(); assertEquals(expectedCar, actualCar); } // ั‚ะตัั‚ โ„– 4 @Test public void testWithDirector_Construct() { VolvoBuilder volvoBuilder = new VolvoBuilder(); CarBuilderDirector director = new CarBuilderDirector(volvoBuilder); director.construct(); final String expectedCar = "Car{body=LIMOUSINE, color=BLACK, name='Volvo'}"; final String actualCar = volvoBuilder.getCar().toString(); assertEquals(expectedCar, actualCar); } } 

Test number 3

 import org.junit.Test; import static org.mockito.Mockito.*; public class CarBuilderDirectorTest { @Test public void testDirectorConstruct() { BaseCarBuilder mockBaseCarBuilder = mock(BaseCarBuilder.class); CarBuilderDirector director = new CarBuilderDirector(mockBaseCarBuilder); director.construct(); verify(mockBaseCarBuilder).buildColor(); verify(mockBaseCarBuilder).buildBody(); } } 

Closed due to the fact that off-topic participants LEQADA , torokhkun , PashaPash โ™ฆ , Streletz , Athari 14 Nov '15 at 15:03 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - LEQADA, torokhkun, Streletz
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    @Barmaley With all due respect, @Ivan_Belozerov0011 is one of those people on this resource who regularly writes and puts the code, and is not busy choosing the best theme for Sublime and throwing between Django and ASP.NET. What's wrong with that? :) - Costantino Rupert
  • 2
    @ Kotik_hokhet_kushat and this is a githab or something, spread the code here? Suppose that in its first question it updates, I already have a presentiment of v5, v25 and so on, why? - rasmisha
  • one
    It would be better to this, this, this, this, this, this and this closed, honestly. - Costantino Rupert
  • one
    imkho, in research it is necessary. and implement several patterns at once (over time). imkho2. I also did not understand why it was the builder. imkho3. classes themselves, whatever they do and whatever tests they pass, must do something, perform some useful function. Except forString, there is nothing useful here. Shl a need to learn, yes. - Yura Ivanov
  • one
    @ Kotik_khohet_kusat threads can close many (including you). I, as a moderator, usually act only on alarm. Theme @ Ivan_Belozerov 0011 is already fed up with the order - no, it does not violate the forum rules, but, as it were, I am personally slightly bored . Minus his topic, I just wanted to give the hint to the author of the topic that some people do not like it - as a simple user, I think I have that right. I did not abuse my moderator right. - Barmaley

0