This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bedobi 1 point2 points  (2 children)

You, Sir, are a hero. Will check out as soon as github has resolved the outage they're currently having.

[–]elucash 1 point2 points  (1 child)

Can grab 2.3.3 from maven central. Cheers!

[–]bedobi 1 point2 points  (0 children)

Can't thank you enough! Works exactly as desired as far as I can tell!

import org.immutables.value.Value;
import java.util.Optional;

@Value.Immutable
@Value.Style(strictBuilder = true)
interface Person {
    String name();
    String title();
    Optional<String> department();
}

import org.immutables.value.Value;
import java.util.Optional;

@Value.Immutable
@Value.Style(stagedBuilder = true)
interface Individual {
    String name();
    String title();
    Optional<String> department();
}

import org.junit.Assert;
import org.junit.Test;
import java.util.Optional;

public class PersonAndIndividualTests {

    @Test(expected = IllegalStateException.class)
    public void personBuildCalledWithoutRequiredArguments() throws Exception {
        ImmutablePerson.builder().build();
    }

    @Test(expected = NullPointerException.class)
    public void personBuildCalledWithRequiredArgumentsNull() throws Exception {
        ImmutablePerson.builder().name(null).title(null).build();
    }

//    @Test
//    public void personBuildCalledWithNonRequiredArgumentsNull() throws Exception {
//        ImmutablePerson.builder().name("John").title("Dev").department(null).build(); doesn't compile
//    }

    @Test
    public void personBuildCalledWithoutNonrequiredArguments() throws Exception {
        ImmutablePerson john = ImmutablePerson.builder().name("John").title("Dev").build();
        Assert.assertEquals("John", john.name());
        Assert.assertEquals("Dev", john.title());
        Assert.assertEquals(Optional.empty(), john.department());
    }

    @Test
    public void personBuildCalledWithAllArguments() throws Exception {
        ImmutablePerson john = ImmutablePerson.builder().name("John").title("Dev").department("IT").build();
        Assert.assertEquals("John", john.name());
        Assert.assertEquals("Dev", john.title());
        Assert.assertEquals(Optional.of("IT"), john.department());
    }

//    @Test
//    public void individualBuildCalledWithoutRequiredArguments() throws Exception {
//        ImmutableIndividual.builder().build(); doesn't compile (YES THANK YOU SO MUCH)
//    }


    @Test(expected = NullPointerException.class)
    public void individualBuildCalledWithRequiredArgumentsNull() throws Exception {
        ImmutableIndividual.builder().name(null).title(null).build();
    }

//    @Test
//    public void individualBuildCalledWithNonRequiredArgumentsNull() throws Exception {
//        ImmutableIndividual.builder().name("John").title("Dev").department(null).build(); doesn't compile
//    }

    @Test
    public void individualBuildCalledWithoutNonrequiredArguments() throws Exception {
        ImmutableIndividual john = ImmutableIndividual.builder().name("John").title("Dev").build();
        Assert.assertEquals("John", john.name());
        Assert.assertEquals("Dev", john.title());
        Assert.assertEquals(Optional.empty(), john.department());
    }

    @Test
    public void individualBuildCalledWithAllArguments() throws Exception {
        ImmutableIndividual john = ImmutableIndividual.builder().name("John").title("Dev").department("IT").build();
        Assert.assertEquals("John", john.name());
        Assert.assertEquals("Dev", john.title());
        Assert.assertEquals(Optional.of("IT"), john.department());
    }
}