I am trying to use Mockito to mock a "Reader" type class. Think of a data stream reader, it has methods to read various data types and advance the internal pointer after each read.
public interface Reader {
int readInt();
short readShort();
}
The class being tested reads in various data structures from the data stream. For example,
public class Somethings {
public List<Something> somethings;
public Somethings(Reader reader) {
somethings = new List<Something>();
int count = reader.readInt();
for (int i=0; i<count; i++) {
somethings.add(readSomething(reader));
}
}
private Something readSomething(Reader reader) {
int a = reader.readInt();
short b = reader.readShort();
int c = reader.readInt();
return new Something(a, b, c);
}
}
And finally, I have my test:
public class SomethingsTest {
#Test
public void testSomethings() {
Reader reader = Mockito.mock(Reader.class);
readCount(reader, 2);
readSomething(reader, 1, 2, 3);
readSomething(reader, 4, 5, 6);
Somethings somethings = new Somethings(reader);
Assert.assertEqual(2, somethings.size());
Assert.assertEquals(new Something(1, 2, 3), somethings.somethings.get(0));
Assert.assertEquals(new Something(4, 5, 6), somethings.somethings.get(1));
}
private void readCount(Reader reader, int count) {
when(reader.readInt()).thenReturn(count);
}
private void readSomething(Reader reader, int a, short b, int c) {
when(reader.readInt()).thenReturn(a);
when(reader.readShort()).thenReturn(b);
when(reader.readInt()).thenReturn(c);
}
}
Unfortunately, this does not work. reader.readInt() always returns 6 for every invocation. And I understand why it returns 6. That is not my question.
There are two options I can think of to fix the tests, but I don't particularly like either one.
The first option would be something like:
public class SomethingsTest {
#Test
public void testSomethings() {
Reader reader = Mockito.mock(Reader.class);
when(reader.readInt())
.thenReturn(2)
.thenReturn(1)
.thenReturn(3)
.thenReturn(4)
.thenReturn(6);
when(reader.readShort())
.thenReturn(2)
.thenReturn(5);
Somethings somethings = new Somethings(reader);
Assert.assertEqual(2, somethings.size());
Assert.assertEquals(new Something(1, 2, 3), somethings.somethings.get(0));
Assert.assertEquals(new Something(4, 5, 6), somethings.somethings.get(1));
}
}
This should work, but it's very monolithic and messy. It's difficult to see which return is for which piece of which structure, because they're all intermixed, with no structure.
The second option I can think of is something like:
public class SomethingsTest {
#Test
public void testSomethings() {
Reader reader = Mockito.mock(Reader.class);
NewOngoingStubbing readIntStub = when(reader.readInt());
NewOngoingStubbing readShortStub = when(reader.readShort());
readCount(readIntStub, 2);
readSomething(readIntStub, readShortStub, 1, 2, 3);
readSomething(readIntStub, readShortStub, 4, 5, 6);
Somethings somethings = new Somethings(reader);
Assert.assertEqual(2, somethings.size());
Assert.assertEquals(new Something(1, 2, 3), somethings.somethings.get(0));
Assert.assertEquals(new Something(4, 5, 6), somethings.somethings.get(1));
}
private void readCount(NewOngoingStubbing readIntStub, int count) {
readIntStub.thenReturn(count);
}
private void readSomething(NewOngoingStubbing readIntStub,
NewOngoingStubbing readShortStub, int a, short b, int c) {
readIntStub.thenReturn(a);
readShortStub.thenReturn(b);
readIntStub.thenReturn(c);
}
}
This at least maintains the structure of the original, but having to pass a separate object for each method call you want to make on the stubbed object is... ugh.
What would be the cleanest way to perform this test? Is there some option I'm missing here? Some functionality that I can leverage? I just started using Mockito tonight.. so I could very well be missing something.
Mockito does ongoing stubbing natively. Your first example is fine, but this should also work:
when(reader.readInt()).thenReturn(2, 1, 3, 4, 6);
The documentation for it is here.
If you have something handling particularly complex interaction, it's OK to roll out your own stub class. You may find that initialising some fake with realistic data, then using that, provides a clearer example of how the classes collaborate than Mockito can. If that's the case, go with clarity over convention. Mockito is IMO the best mocking framework out there, but sometimes I still roll my own.
When the standard methods for Mockito don't provide a mechanism to simulate your behavior, you can resort to implementing your own Answer. This is more work but provides extra flexibility.
Depending on your precise requirements, you could for instance create an Answer that returns a new element from a list of numbers, regardless of the request type (int or short). The variable readList can be a member that you can access from all the functions you use to set up your results.
final List<Integer> readList = new ArrayList<>();
// ... Fill readList with answers
Answer answerFromList = new Answer() {
Object answer(InvocationOnMock invocation) {
// Remove and return first element
return readList.remove(0);
}
}
when(reader.readInt()).thenAnswer(answerFromList);
when(reader.readShort()).thenAnswer(answerFromList);
Note that this Answer is a lot like the provided ReturnElementsOf, so you could use that directly as well.
You may create a class to deal with it, e.g.
private static class ReaderStubber {
private final Reader reader = Mockito.mock(Reader.class);
private final NewOngoingStubbing readIntStub = when(reader.readInt());;
private final NewOngoingStubbing readShortStub = when(reader.readShort());;
public Reader getReader() {
return reader;
}
private void readCount(int count) {
readIntStub.thenReturn(count);
}
private void readSomething(int a, short b, int c) {
readIntStub.thenReturn(a);
readShortStub.thenReturn(b);
readIntStub.thenReturn(c);
}
}
But then the question is, do you really need to do this with Mockito? Not everything should be mocked. Maybe just implementing a stub Reader for the test with some List<Integer> inside is better.
(Edit)
Also, if this is possible, maybe you should redesign the Reader to make it immutable and return some NewOngoingReading. Frequently (but not always) things that are hard to test are better to redesign. Also, you will not need to deal with synchronization.
Related
Just for reference, I've taken like one high school class on Java, so this question should be super easy to respond to. I'm just having trouble figuring out a way to call a class method, the constructor, as a new object without putting in new values for the parameters. The constructor looks like this:
public Skills(int courage, int intelligence, int stamina, int crafting,
int blacksmithery, int herbalism, int slingSkill,
int bowSkill, int swordSkill, int armor, int stealth, int
lifeForceSkill){
this.courage = courage;
this.intelligence = intelligence;
this.stamina = stamina;
this.crafting = crafting;
this.blacksmithery = blacksmithery;
this.herbalism = herbalism;
this.slingSkill = slingSkill;
this.bowSkill = bowSkill;
this.swordSkill = swordSkill;
this.armor = armor;
this.stealth = stealth;
this.lifeForceSkill = lifeForceSkill;
}
And when I establish it it my main method I do this:
Skills skills = new Skills(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
To set all the levels to 1 and then I have the object skills to work with later. (Am I getting the terms constructors and objects mixed up? If it's wrong just switch them all in your mind I think I'm being consistent with it at least).
The problem is I have a toString() method in the Skills class that I want to call in a separate method in the main class.
else if (optionChoice.equalsIgnoreCase("View Skills")){
Skills skills = new Skills();
System.out.println(skills.toString());
break;
}
Of course the object I create here throws an error because I cannot just write Skills(); without putting in all twelve values for the parameter. So how do I call the method from the Skills class when I can't create an object for it? It's imperative to functionality that the values do not change, and since they are variable and change with the program, I can't put any value in for them.
I know it's probably a super basic question but that's the level I'm at so any super basic answers would be helpful. Shouldn't take too much of your time. Thanks in advance!
Why not just make your skills object a static field instead of creating it within the main method?
public class WhateverYourClassIsCalled {
static Skills skills = new Skills(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
public static void main(String[] args) {
// ...
}
void yourOtherMethod() {
// ...
else if (optionChoice.equalsIgnoreCase("View Skills")) {
System.out.println(skills.toString());
break;
}
}
}
I have looked at several posts to reach an answer, but none came close enough.
I have a Q that might sound very empirical, and I am sure the answer is staring me in the face, but I am blinkered, I suppose! I have a class named DrugExcretion with a constructor that has a parameter that is a reference to an array (named drugExcretionCode).
I can't seem to work out the SYNTAX to instantiate (i.e. create an object) in my main class named DrugExcretionApp. Both classes are below:-
If anyone can direct me to a simple way to do this, it would be much appreciated. Thanks in advance.
public class DrugExcretion implements CautionInterface{
int[] drugExcretionCode;
private String[] drug;
public DrugExcretion(String[] drug){
this.drug = drug;
}
public String determineDanger(int[] drugExcretionCode){
String site1 = "kidney";
String site2 = "liver";
String site;
if (drugExcretionCode = 1){
return "reduce dosage in elderly";
}
else{
return "reduce dosage in children";
}
}
}
x ----------------- o ----------------- x ----------------- o
public class drugExcretionApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
final String drug1 = "enalapril";
final String drug2 = "captopril";
final String drug3 = "metoprolol";
final String drug4 = "amlodipine";
final String drug5 = "candesartan";
String drug[] = {drug1, drug2, drug3, drug4, drug5};
DrugExcretion listOne = new DrugExcretion(drug);
DrugExcretion.determineDanger(new int[]{1, 1, 2, 2, 1});
}
}
I get an error "DrugExcretion cannot be resolved to a type" which initially made me think that the project setup was incorrect so the DrugExcretion class could not be seen by the app class - I rearranged the classes to keep them in the same package, but the error persists.
It appears to be a problem with access modifiers - have I made something static inadvertently???
BTW, the interface simply contains a signature of the only method in the first class.
You have to call determineDanger like this:
listOne.determineDanger(new int[]{1, 1, 2, 2, 1]);
DrugExcretion.xxx would be for a static method xxx of DrugExcretion.
But determineDanger is an instance method, so needs to be invoked on an instance of the class, in this case listOne.
Since you are instantiating the DrugExcretion class, you need to call
listOne.determineDanger(new int[] { 1, 1, 2, 2, 1 });
Also, please check your code in the DrugExcretion class. It should be equals operator and not assignment.
if (drugExcretionCode == 1){
Consider following code as totally simplified example of the idea:
public class Main {
public static void foreach(int[] x, intVoid action) {
for (int i = 0; i < x.length; ++i) {
action.apply(x[i]);
}
}
public static void main (String[] args) {
int[] h = new int[] {2, 3, 5, 7, 11, 13};
for (int i = 0; i < 10; ++i) {
foreach(h, new intVoid() {
public void apply(int x) {
System.out.println(x * 2);
}
});
}
}
}
interface intVoid {public void apply(int x);}
In the for loop we call foreach with absolutely identical arguments from logical point of view, since the anonymous implementation of our interface doesn't depend of anything in the context. The question is - will it be instantinated 10 times or only once? Or, similar, will it be equivalent for the following code:
public class Main {
public static void foreach(int[] x, intVoid action) {
for (int i = 0; i < x.length; ++i) {
action.apply(x[i]);
}
}
public static void main (String[] args) {
int[] h = new int[] {2, 3, 5, 7, 11, 13};
intVoid action = new intVoid() {
public void apply(int x) {
System.out.println(x * 2);
}
};
for (int i = 0; i < 10; ++i) {
foreach(h, action);
}
}
}
interface intVoid {public void apply(int x);}
I sometimes had been in such situations, where it's very convenient to define an implementation exactly in place where it's needed, but sometimes I also need to be sure that there will be no attempt to create the same object multiple times.
If there is an optimization in the runtime, then I'm interested in how this is handled in different implementations, especially what will happen if I convert such code to be run in Android's Dalvik VM.
I know that I can test this all myself, but I'm wondering if someone has already shed some light on this question.
The question is - will it be instantinated 10 times or only once?
10 times, because that's what you've told it to do. I'd be surprised to see that the JVM (or Dalvik) noticed that apply had no side-effects and that the object had no state, and therefore you didn't need to create separate instances. It's possible - Hotspot's pretty impressive - but it seems an unlikely optimization to me.
Of course, creating each object is probably cheap - but it would still be better to create one object as per your second code snippet.
It is instantiated once for every new performed. It is possible escape analysis will eliminate it after it has been JITed e.g. after 10,000 times but I wouldn't assume that it does.
If it's smart enough not to create separate instances, it's smart enough to eliminate it which is better.
I'm new to the Java language and I've tried to write my first relatively complex program. After I wrote a few classes I've realized that I barely use built-in classes (like BigInteger, MessageDigest, ByteBuffer) directly because they don't totally fit my needs. Instead I write my own class and inside the class I use the built-in class as an attribute.
Example:
public class SHA1 {
public static final int SHA_DIGEST_LENGTH = 20;
private MessageDigest md;
public SHA1() {
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public void update(byte[] data) {
md.update(data);
}
public void update(BigNumber bn) {
md.update(bn.asByteArray());
}
public void update(String data) {
md.update(data.getBytes());
}
public byte[] digest() {
return md.digest();
}
}
With the following simple class I don't have to use try catch when using SHA1, I can put my custom BigNumber class as parameter and I can also put String as parameter to update function.
The following BigNumber class contains all of the functions what I need and exactly how I need them.
public class BigNumber {
private BigInteger m_bn;
public BigNumber() {
m_bn = new BigInteger("0");
}
public BigNumber(BigInteger bn) {
m_bn = bn;
}
public BigNumber(String hex) {
setHexStr(hex);
}
//reversed no minsize
public byte[] asByteArray() {
return asByteArray(0, true);
}
//reversed with minsize
public byte[] asByteArray(int minSize) {
return asByteArray(minSize, true);
}
public byte[] asByteArray(int minSize, boolean rev) {
byte[] mag = m_bn.toByteArray();
//delete sign bit
//there is always a sign bit! so if bitNum % 8 is zero then
//the sign bit created a new byte (0th)
if(getNumBits() % 8 == 0) {
byte[] tmp = new byte[mag.length-1];
System.arraycopy(mag, 1, tmp, 0, mag.length-1);
mag = tmp;
}
//extend the byte array if needed
int byteSize = (minSize >= getNumBytes()) ? minSize : getNumBytes();
byte[] tmp = new byte[byteSize];
//if tmp's length smaller then byteSize then we keep 0x00-s from left
System.arraycopy(mag, 0, tmp, byteSize-mag.length, mag.length);
if(rev) ByteManip.reverse(tmp);
return tmp;
}
public String asHexStr() {
return ByteManip.byteArrayToHexStr(asByteArray(0, false));
}
public void setHexStr(String hex) {
m_bn = new BigInteger(hex, 16);
}
public void setBinary(byte[] data) {
//reverse = true
ByteManip.reverse(data);
//set as hex (binary set has some bug with the sign bit...)
m_bn = new BigInteger(ByteManip.byteArrayToHexStr(data), 16);
}
public void setRand(int byteSize) {
byte[] tmp = new byte[byteSize];
new Random().nextBytes(tmp);
//reversing byte order, but it doesn't really matter since it is a random
//number
setBinary(tmp);
}
public int getNumBytes() {
return (m_bn.bitLength() % 8 == 0) ? (m_bn.bitLength() / 8) : (m_bn.bitLength() / 8 + 1);
}
public int getNumBits() {
return m_bn.bitLength();
}
public boolean isZero() {
return m_bn.equals(BigInteger.ZERO);
}
//operations
public BigNumber modExp(BigNumber exp, BigNumber mod) {
return new BigNumber(m_bn.modPow(exp.m_bn, mod.m_bn));
}
public BigNumber mod(BigNumber m) {
return new BigNumber(m_bn.mod(m.m_bn));
}
public BigNumber add(BigNumber bn) {
return new BigNumber(m_bn.add(bn.m_bn));
}
public BigNumber subtract(BigNumber bn) {
return new BigNumber(m_bn.subtract(bn.m_bn));
}
public BigNumber multiply(BigNumber bn) {
return new BigNumber(m_bn.multiply(bn.m_bn));
}
}
My question is that how common in Java language to use these kind of classes instead of the built-in classes? Does it make my code unreadable for other programmers (compared to implementing everything with built-in classes)?
I've read that new C++ programmers desperately trying to write codes they used to write in C therefore the benefits of C++ remains hidden for them.
I'm afraid I do something like that in Java: trying to implement everything on my own instead of using the build-in classes directly.
Is this happening (for example in the BigNumber class)?
Thank you for your opinions!
I normally write a utility class which will support me to handle logics. Such as
public class CommonUtil{
public byte[] asByteArray(int minSize)
{
return "something".getBytes();
}
// add more utility methods
}
Wrapping a class makes sense when you add some value by doing so. If you are adding small functionality it can be worth using a Utility class instead of wrapping an existing one.
I think that if you do not have a very good reason for implementing the same functionality again you should not probably do it. Here are several reasons:
Built-in classes are used by a lot of people around the world and therefore there are less bugs than in your code
Users that are experienced in Java will be better in using standard classes than your classes and they will need less time to understand your code and write something new in your project
Built-in classes have good documentations and therefore it is much easier to use them
You are wasting your time by implementing something that was implemented and tested by Java professionals. It is better to concentrate on your own project
If you are writing a long-term project you will need to support all your classes. Oracle is already supporting built-in classes. Let them do their job!
The last but not the least. Are you sure that you know more about the problem than an author of a built-in class? Only if the answer is yes, consider writing your own implementation. Even implementation of daily used classes, such as collections or time-related classes can be tricky.
You're not gaining anything by making a class that does this stuff for you. If you're going to be doing certain operations a lot, then you might want to create a new class with static methods that do these important things for you.
Let's assume that you want a sorted array at all times. You could make a new class, let's call it SortedArray. You could sort it whenever you add something in, but why would you do that when you can just add in everything and then call the (utility) method Arrays.sort?
For common operations, take a look at Java's Arrays class - if you are doing something often, that's something you make a method for, like searching and sorting. In your case, you might make a utility method that turns the BigInteger into a byte array for you. You shouldn't be just making your own, 'better' version that does what you want it. When other people look at your code, when you use standard objects it's much better, instead of having custom objects that don't really do anything.
As #Shark commented, there's no point in creating your own solutions, because:
They take time to create
They become not as flexible
However, you can extend classes (it's recommended) or use 3rd party frameworks that might suit you better.
I'm trying to construct a PackingCase object with a certain set of values. While the program shows no errors during coding, when running, I get this error;
Exception in thread "main" java.lang.StackOverflowError
at assignment.pkg2.PackingCase.<init>(PackingCase.java:59)
at assignment.pkg2.PackingCase.<init>(PackingCase.java:60)
My code is as follows;
public class PackingCase {
// private fields go here
int serialNumber;
int timesUsed;
int timeCreated;
int timeStored;
String name;
String description;
void setCase(int s, int TU, int TC, int TS){
serialNumber = s;
timesUsed = TU;
timeCreated = TC;
timeStored = TS;
}
double volume(){
return serialNumber*timesUsed*timeCreated*timeStored;
}
public PackingCase(){
PackingCase PC1 = new PackingCase();
double vol;
PC1.setCase(1, 2, 3, 4);
vol = PC1.volume();
System.out.println(""+vol);
}
Line 59 is "public PackingCase(){" , and Line 60 is "PackingCase PC1 = new PackingCase();". I have no idea what's going on, considering that an example I found uses virtually the same code structure, and compiles with no errors whatever. Any help would be appreciated.
Each creation of a new object leads to the creation of another new object (and so on...) until the stack is overflowed.
Instead, it should be look like that:
public PackingCase(){
this.setCase(1, 2, 3, 4);
vol = this.volume();
System.out.println(""+vol);
}
You have a recursive call in the constructor. Leave the constructor empty (simply delete it) and run this code from main method:
public static void main(String[] a){
PackingCase pc1 = new PackingCase();
pc1.setCase(1, 2, 3, 4);
double vol = pc1.volume();
System.out.println(""+vol);
}
public PackingCase(){ PackingCase PC1 = new PackingCase(); ...}
Constructor recursively calls itself, causing stackoverflow.
You are calling new within the handler for new, creating an infinite loop (and since the stack is finite, it eventually runs out of space). But public PackingCase() { ... } is a constructor. That means it is only called when someone has already used new PackingCase(). The code within the constructor doesn't have to create the object (allocate space), just initialize it (set values for its fields).