Skip to content

Commit

Permalink
Adds tests for FieldMutator
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Sep 19, 2024
1 parent d8b17c3 commit 2e323b3
Showing 1 changed file with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package nl.jqno.equalsverifier.internal.reflection;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class FieldMutatorTest {

private FieldProbe p;
private FieldMutator sut;
private Container o = new Container();

@Test
public void setPrimitive() throws NoSuchFieldException {
p = FieldProbe.of(Container.class.getDeclaredField("i"));
sut = new FieldMutator(p);

assertEquals(10, o.i);
sut.setNewValue(o, 1337);
assertEquals(1337, o.i);
}

@Test
public void setObject() throws NoSuchFieldException {
p = FieldProbe.of(Container.class.getDeclaredField("s"));
sut = new FieldMutator(p);

assertEquals("NON-FINAL", o.s);
sut.setNewValue(o, "changed");
assertEquals("changed", o.s);
}

@Test
public void dontSetConstantPrimitive() throws NoSuchFieldException {
p = FieldProbe.of(Container.class.getDeclaredField("FINAL_INT"));
sut = new FieldMutator(p);

assertEquals(42, Container.FINAL_INT);
sut.setNewValue(o, 1337);
assertEquals(42, Container.FINAL_INT);
}

@Test
public void dontSetConstantObject() throws NoSuchFieldException {
p = FieldProbe.of(Container.class.getDeclaredField("FINAL_STRING"));
sut = new FieldMutator(p);

assertEquals("FINAL", Container.FINAL_STRING);
sut.setNewValue(o, "changed");
assertEquals("FINAL", Container.FINAL_STRING);
}

static class Container {

private static final int FINAL_INT = 42;
private static final String FINAL_STRING = "FINAL";
private final int i;
private final String s;

public Container() {
this.i = 10;
this.s = "NON-FINAL";
}
}
}

0 comments on commit 2e323b3

Please sign in to comment.