Skip to content

Commit

Permalink
Migrate some unit tests that are using jMock to JUnit Jupiter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeantessier committed Jan 10, 2025
1 parent 07e8008 commit 13ce86e
Show file tree
Hide file tree
Showing 97 changed files with 1,440 additions and 1,261 deletions.
5 changes: 5 additions & 0 deletions lib/src/test/java/com/jeantessier/MockObjectTestCase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jeantessier;

import org.jmock.Sequence;
import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.jmock.internal.ExpectationBuilder;
import org.jmock.junit5.JUnit5Mockery;
Expand All @@ -19,6 +20,10 @@ protected <T> T mock(Class<T> typeToMock, String name) {
return context.mock(typeToMock, name);
}

protected Sequence sequence(String name) {
return context.sequence(name);
}

protected void checking(ExpectationBuilder expectations) {
context.checking(expectations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,35 @@
package com.jeantessier.classreader.impl;

import org.jmock.*;
import org.junit.jupiter.api.*;

import com.jeantessier.classreader.*;

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

public class TestAnnotation extends TestAnnotationsBase {
private static final int TYPE_INDEX = 2;
private static final String RAW_TYPE = "Labc;";
private static final String TYPE = "abc";

private Annotation sut;

protected void setUp() throws Exception {
super.setUp();

@BeforeEach
void setUp() throws Exception {
expectReadAnnotation(TYPE_INDEX, 0);
expectLookupUtf8(TYPE_INDEX, RAW_TYPE, "lookup during construction");

sut = new Annotation(mockConstantPool, mockIn);
}

public void testGetType() throws Exception {
@Test
void testGetType() throws Exception {
expectLookupUtf8(TYPE_INDEX, RAW_TYPE);
assertEquals(TYPE, sut.getType());
}

public void testAccept() {
@Test
void testAccept() {
final Visitor mockVisitor = mock(Visitor.class);

checking(new Expectations() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@
package com.jeantessier.classreader.impl;

import org.jmock.*;
import org.junit.jupiter.api.*;

import com.jeantessier.classreader.*;

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

public class TestAnnotationDefault_attribute extends TestAnnotationsBase {
private ElementValue mockElementValue;

private AnnotationDefault_attribute sut;

protected void setUp() throws Exception {
super.setUp();

@BeforeEach
void setUp() throws Exception {
expectReadAttributeLength(3);

mockElementValue = mock(ElementValue.class);
Expand All @@ -56,15 +58,18 @@ protected void setUp() throws Exception {
sut = new AnnotationDefault_attribute(mockConstantPool, mockOwner, mockIn, mockElementValueFactory);
}

public void testGetElementValue() {
@Test
void testGetElementValue() {
assertSame(mockElementValue, sut.getElemementValue());
}

public void testGetAttributeName() {
@Test
void testGetAttributeName() {
assertEquals(AttributeType.ANNOTATION_DEFAULT.getAttributeName(), sut.getAttributeName());
}

public void testAccept() {
@Test
void testAccept() {
final Visitor mockVisitor = mock(Visitor.class);

checking(new Expectations() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,40 @@
import java.io.*;

import org.jmock.*;
import org.junit.jupiter.api.*;

import com.jeantessier.classreader.*;

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

public class TestAnnotationElementValue extends TestAnnotationsBase {
private static final int TYPE_INDEX = 2;
private static final String TYPE = "Labc;";

private AnnotationElementValue sut;

protected void setUp() throws Exception {
super.setUp();

@BeforeEach
void setUp() throws Exception {
expectReadAnnotation(TYPE_INDEX, 0);
expectLookupUtf8(TYPE_INDEX, TYPE);

sut = new AnnotationElementValue(mockConstantPool, mockIn);
}

public void testGetAnnotation() throws IOException {
@Test
void testGetAnnotation() throws IOException {
assertEquals(TYPE_INDEX, sut.getAnnotation().getTypeIndex());
assertEquals(0, sut.getAnnotation().getElementValuePairs().size());
}

public void testGetTag() {
@Test
void testGetTag() {
assertEquals(ElementValueType.ANNOTATION.getTag(), sut.getTag());
}

public void testAccept() {
final Visitor mockVisitor = mock(Visitor.class);
@Test
void testAccept() {
var mockVisitor = mock(Visitor.class);

checking(new Expectations() {{
oneOf (mockVisitor).visitAnnotationElementValue(sut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@
import java.io.*;

import org.jmock.*;
import org.junit.jupiter.api.*;

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

public class TestAnnotationWithElementValues extends TestAnnotationsBase {
private static final int TYPE_INDEX = 2;
private static final String TYPE = "Labc;";

public void testConstructorWithNoElementValuePairs() throws Exception {
@Test
void testConstructorWithNoElementValuePairs() throws Exception {
doTestConstructorWithElementValuePairs(0);
}

public void testConstructorWithASingleElementValuePair() throws Exception {
@Test
void testConstructorWithASingleElementValuePair() throws Exception {
doTestConstructorWithElementValuePairs(1);
}

public void testConstructorWithMultipleElementValuePairs() throws Exception {
@Test
void testConstructorWithMultipleElementValuePairs() throws Exception {
doTestConstructorWithElementValuePairs(2);
}

Expand All @@ -73,6 +79,6 @@ private void doTestConstructorWithElementValuePairs(final int numElementValuePai
}});

Annotation sut = new Annotation(mockConstantPool, mockIn, mockElementValueFactory);
assertEquals("Num element value pairs", numElementValuePairs, sut.getElementValuePairs().size());
assertEquals(numElementValuePairs, sut.getElementValuePairs().size(), "Num element value pairs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@
import java.io.*;

public abstract class TestAnnotationsBase extends TestAttributeBase {
protected ElementValueFactory mockElementValueFactory;

protected void setUp() throws Exception {
super.setUp();

mockElementValueFactory = mock(ElementValueFactory.class);
}
protected ElementValueFactory mockElementValueFactory = mock(ElementValueFactory.class);

protected void expectReadNumValues(int numValues) throws IOException {
expectReadU2(numValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,29 @@
package com.jeantessier.classreader.impl;

import org.jmock.*;
import org.junit.jupiter.api.*;

import com.jeantessier.classreader.*;

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

public class TestArrayElementValue extends TestAnnotationsBase {
private ArrayElementValue sut;

protected void setUp() throws Exception {
super.setUp();

@BeforeEach
void setUp() throws Exception {
expectReadNumValues(0);

sut = new ArrayElementValue(mockConstantPool, mockIn, mockElementValueFactory);
}

public void testGetTag() {
@Test
void testGetTag() {
assertEquals(ElementValueType.ARRAY.getTag(), sut.getTag());
}

public void testAccept() {
@Test
void testAccept() {
final Visitor mockVisitor = mock(Visitor.class);

checking(new Expectations() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,23 @@
import java.io.*;

import org.jmock.*;
import org.junit.jupiter.api.*;

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

public class TestArrayElementValueWithContent extends TestAnnotationsBase {
public void testConstructorWithNoValues() throws Exception {
@Test
void testConstructorWithNoValues() throws Exception {
doTestConstructorWithValues(0);
}

public void testConstructorWithASingleValue() throws Exception {
@Test
void testConstructorWithASingleValue() throws Exception {
doTestConstructorWithValues(1);
}

public void testConstructorWithMultipleValues() throws Exception {
@Test
void testConstructorWithMultipleValues() throws Exception {
doTestConstructorWithValues(2);
}

Expand All @@ -57,6 +63,6 @@ private void doTestConstructorWithValues(final int numValues) throws IOException
}});

ArrayElementValue sut = new ArrayElementValue(mockConstantPool, mockIn, mockElementValueFactory);
assertEquals("Num values", numValues, sut.getValues().size());
assertEquals(numValues, sut.getValues().size(), "Num values");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,21 @@

package com.jeantessier.classreader.impl;

import com.jeantessier.MockObjectTestCase;
import com.jeantessier.classreader.Visitable;
import org.jmock.Expectations;
import org.jmock.Sequence;
import org.jmock.imposters.ByteBuddyClassImposteriser;
import org.jmock.integration.junit3.MockObjectTestCase;

import java.io.DataInput;
import java.io.IOException;

public abstract class TestAttributeBase extends MockObjectTestCase {
protected Classfile mockClassfile;
protected ConstantPool mockConstantPool;
protected Visitable mockOwner;
protected DataInput mockIn;
protected Classfile mockClassfile = mock(Classfile.class);
protected ConstantPool mockConstantPool = mock(ConstantPool.class);
protected Visitable mockOwner = mock(Visitable.class);
protected DataInput mockIn = mock(DataInput.class);

protected Sequence dataReads;

protected void setUp() throws Exception {
super.setUp();

setImposteriser(ByteBuddyClassImposteriser.INSTANCE);

mockClassfile = mock(Classfile.class);
mockConstantPool = mock(ConstantPool.class);
mockOwner = mock(Visitable.class);
mockIn = mock(DataInput.class);

dataReads = sequence("dataReads");
}
protected Sequence dataReads = sequence("dataReads");

protected void allowingLookupModule(final int index, final String value) {
allowingLookupModule(index, value, mock(Module_info.class));
Expand Down
Loading

0 comments on commit 13ce86e

Please sign in to comment.