I'm new at unit testing and am running into an issue with jMock that I can't seem to figure out. I have a public final instance variable which I need to define an expectation for, but I can't get it to work. If I make a getter for the variable, it works, but I'd rather not have to create a bunch of getters just to make unit testing work. Any help on how to do this would be much appreciated. Here's some code illustraiting what I'm trying to do:
public class Main {
private SimpleObject simpleObject;
public Main(SimpleObject o){
this.simpleObject = o;
}
public int iDontWork(){
return simpleObject.myList.size();
}
public int iWork(){
return simpleObject.getMyList().size();
}
}
My test:
#RunWith(JMock.class)
public class MainTest {
Mockery context = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
#Mock
SimpleObject simpleObject;
private Main main;
#Before
public void setup(){
main = new Main(simpleObject);
}
#Test
public void itWorks() {
context.checking(new Expectations() {{
oneOf(simpleObject).getMyList();
will(returnValue(new ArrayList<String>(Arrays.asList("Hey"))));
}});
int i = main.iWork();
assertEquals(1, i);
}
#Test
public void itDoesntWork() {
context.checking(new Expectations() {{
oneOf(simpleObject).myList.size(); will(returnValue(1));
}});
int i = main.iDontWork();
assertEquals(1, i);
}
}
SimpleObject:
public class SimpleObject {
public final List<String> myList;
public SimpleObject(){
myList = Collections.unmodifiableList(Arrays.asList("Hey"));
}
public List<String> getMyList(){
return myList;
}
}
A mock object implements methods of the real object. It does not have fields of the real object (even if these fields are public).
Related
I am having a protected method inside a static child class. I am running a testcase , its getting successful but code coverage is not increasing.
public class A{
private static final String var1 = "key1";
protected static class B extends OsCmd{
private String abc1;
private String abc2;
protected B(String xyz, String xyz2) {
this.abc1 = xyz;
this.abc2 = xyz2;
}
#Override
protected void updateEnv(Map<String, String> env) {
env.put(VAR1, "FALSE");
env.put(VAR2, "TRUE");
env.put("key3", abc1);
env.put("key4", abc2);
}
}
}
Below is my test case
#ExtendWith(MockitoExtension.class)
public class ATest {
private A mockA;
#BeforeEach
public void setup() {
mockA = Mockito.spy(A.class);
}
#Test
public void test2() {
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class)) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
A.B mockB =
new A.B("a", "b");
//doNothing().when(mockB).updateEnv(map);
mockB.updateEnv(map);
}
}
}
Can someone please help here, what mistake i am doing?
When you mock the constructor, then all internal method calls are also mocked and do not go through the actual code.
If you remove the following try-with-resources:
try (MockedConstruction mockedConstruction =
mockConstruction(A.B.class))
The real code will be executed and the coverage will increase.
For example I have some tests based on Set<Integer>. I want to run them with TreeSet and then with HashSet. Can I do it without manual initialization inside test method body?
Something like this:
public class SomeTest {
Set<Integer> set;
#Before
public void init() {
set = new HashSet<>();
}
// #Before
// public void init2() {
// set = new TreeSet<>();
// }
//test...
}
I want to run all tests with init() first and then with init2(). How can I do it?
A cleaner approach would be:
public abstract class SomeTestsForSets {
Set<Integer> set;
#Before
public abstract void init();
//test cases...
}
public class HashSetTests extends SomeTestsForSets {
#Override
public void init() {
this.set = new HashSet<>();
}
}
public class TreeSetTests extends SomeTestsForSets {
#Override
public void init() {
this.set = new TreeSet<>();
}
}
Consider the following code:
#Tested
CodeToTest codeToTest;
#Injectable
Injected injected;
#Test
public void test() {
new Expectations() {{ ... }}
assertThat(codeToTest.memberVariable).isEqualTo("x");
}
//
public class CodeToTest { public CodeToTest(Injected injected) { memberVariable = injected.getProperty("x") } }
I want to test CodeToTest. CodeToTest needs Injected to be injected into it's constructor. How do I set a property such as injected.setProperty("x") so that it is available for access in CodeToTest?
The test at hand should be covering a specific method of CodeToTest; the constructor should have its own tests like your test does. So for example, if the constructor is setting a field according to what gets passed in, like this:
public class Bar {
public int getValue() {
return 8675309;
}
}
public class Foo {
public String field = null;
public Foo(Bar b) {
this.field = "" + b.getValue();
}
public String getField() {
return this.field;
}
public char getFirstChar() {
return getField().charAt(0);
}
}
Here I am setting a String field according to an int found in a Bar that was passed into the constructor. I wish to unit test my getFirstChar() method ...
#Tested Foo foo;
#Injectable Bar bar;
#Test
public void test() throws Exception {
// ...
}
Now, as you point out, in this case my field has already been set before test() even starts. So I have two choices here: Firstly, since I am pulling out the field based on its getter, I can partially mock my class being tested:
#Test
public void test() throws Exception {
new Expectations(foo) {{
foo.getField(); result = "24601";
}};
char c = foo.getFirstChar();
assertThat(c, is('2'));
}
OR, if you don't want to do this or if you are doing direct field access rather than via getter, you can use Deencapsulation (part of JMockit) to set the internal field and then test:
#Test
public void test() throws Exception {
Deencapsulation.setField(foo, "field", "24601");
char c = foo.getFirstChar();
assertThat(c, is('2'));
}
And of course, I test my constructor separately:
#Test
public void testConstructor() throws Exception {
new Expectations() {{
bar.getValue(); result = 24601;
}};
Foo foo2 = new Foo(bar);
String fieldValue = foo2.getField(); // or use Deencapsulation
assertThat(fieldValue, is("24601"));
}
Hope this helps, and good luck!
I have a constructor that calls a method, like this:
public Foo(boolean runExtraStuff) {
if (runExtraStuff){
doExtraStuff();
}
}
The doExtraStuff() method is running some additional commands that are not easily mocked themselves (things like database checks to initialize some variables). Perhaps it would be better for the constructor to not do this, but this is the code I have to work with at the moment.
I would like to create a unit test to make sure that doExtraStuff() is called when the boolean runExtraStuff is true and does not run when the boolean is false. I am using JMockit.
However, I'm not sure how to make this happen. Normally I would use a Verifications on a mocked object, but since I am testing the constructor, I can't use a mocked object in this way. So how can I verify that a method within a constructor was called?
It's easy enough, even if it requires partial mocking:
#Test
public void runsSetupWhenRequestedOnFooInitialization()
{
// Partially mocks the class under test:
new Expectations(Foo.class) {};
final Foo foo = new Foo(true);
// Assuming "setup" is not private (if it is, use Deencapsulation.invoke):
new Verifications() {{ foo.setup(); }};
}
#Test
public void doesNotRunSetupWhenNotRequestedOnFooInitialization()
{
new Expectations(Foo.class) {};
final Foo foo = new Foo(false);
new Verifications() {{ foo.setup(); times = 0; }};
}
Of course, it would probably be better to avoid mocking in a case like this; instead, the test should check the state of the object through getters or other available methods, if at all possible.
Well, the straightforward answer doesn't use JMockit at all..
in src/main/java/example..
package example;
public class Foo {
private boolean setupRan = false;
public Foo(boolean runSetup) {
if (runSetup) setup();
}
public void setup() {
setupRan = true;
}
public boolean getSetupRan() {
return setupRan;
}
}
in src/test/java/example..
package example;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
public class FooTest {
private Foo testSubject;
#Test
public void should_run_setup() {
testSubject = new Foo(true);
assertThat(testSubject.getSetupRan()).isTrue();
}
#Test
public void should_not_run_setup() {
testSubject = new Foo(false);
assertThat(testSubject.getSetupRan()).isFalse();
}
}
I'll go out on a limb and guess that you are interested in a partial mock here:
in src/main/java/example..
package example;
public class Foo1 {
public Foo1(boolean runSetup) {
if (runSetup) setup();
}
public void setup() {
System.out.println("in setup()");
}
}
in src/test/java/example..
package example;
import static org.assertj.core.api.Assertions.*;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Test;
public class Foo1Test {
// hateful partial mocking of test subject!
#Mocked({"setup()"})
private Foo1 testSubject;
#Test
public void should_run_setup() {
new Expectations() {{
testSubject.setup(); // setup() is called
}};
testSubject = new Foo1(true);
}
#Test
public void should_not_run_setup() {
new Expectations() {{
testSubject.setup(); times = 0;
}};
testSubject = new Foo1(false);
}
}
EDIT 1: Note that you won't see the println output since the method was mocked.
EDIT 2: Set expectations for invocations of testSubject.setup() to times = 0 in second test
Please help, this mock isn't working :
class ClassBeingTested {
private AnotherClass anotherClass;
public void someMethod() {
int ans = anotherClass.targetMethod(5);
// Use ans here
}
}
// My test
ClassBeingTested classObject;
AnotherClass anotherClassObject;
#Before
public void setup() {
// Initialize anotherClassObject here
classObject = new ClassBeingTested(anotherClassObject);
new NonStrictExpectations(anotherClassObject) {{
invoke(anotherClassObject, "targetMethod", Integer.class); result = 100;
}};
}
#Test
public void testSomeMethod() {
classObject.someMethod();
}
Mocking worked as soon as I replaced Integer.class with actual expected int value.
new NonStrictExpectations(anotherClassObject) {{
invoke(anotherClassObject, "targetMethod", 5); result = 100;
}};
How about this way of mocking with JMockit?
import mockit.Injectable;
import mockit.Tested;
...
#Tested
ClassBeingTested classObject;
#Injectable
AnotherClass anotherClassObject;
#Before
public void setup() {
new Expectations() {{
anotherClassObject.targetMethod(anyInt); result = 100;
}};
}
#Test
public void testSomeMethod() {
classObject.someMethod();
}