Carl McGraw

+ Follow
since Oct 02, 2014
Carl likes ...
Python VI Editor Linux
Seattle, WA
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Carl McGraw

Is this the standard way of overcoming dependencies for deployments? Or do you usually just have the user specify the class path?
5 years ago
Hello all. I am still new to the world of deployment, packaging, etc.

I am currently operating purely on the CLI, so eclipse/IDEA plugins etc are not a very good option for me. I was hoping more of a pure solution to this problem using only maven.

Preferably I don't want to have my user adding additional things to the class path of the jar to get the code to execute.

So I have the following project:

Then I have the following in my POM.xml

Now all that I want is for the user to be able to execute the jar without having to specific the commons-cli library.

Is there a way I can get maven to handle this for me in a simple fashion?
5 years ago
First things first, welcome to the ranch.

I will try to answer your question to the best of my abilities. But I don't think you're asking quite what you think you want.

Pointdexter Huynh wrote:I honestly don't understand what to do with the while loop and how to find even and odd numbers.

I am confused. So you are effectively using the while loop here:

Then inside you're checking, using modulo:

That statement in the if is the modulo. You are checking if a given value's remainder when divided by two, is 0 (since modulo or % gives you the remainder when divided by the number on the right).

So using mod you can check the remainder of a number. All even numbers by definition are divisible by 2, and all non even (i.e, odd) are not divisible by two.

However there are a couple problems with the code you posted. First you said you wanted:

Pointdexter Huynh wrote:
2. Output all the even numbers between firstNum and secondNum...

So your loop should go from firstNum to secondNum. But in your loop you're comparing to secondNum only.

You should instead consider comparing to firstNum (Because that is what the problem is asking exactly.

But we also see a problem occurring here. Lets look at some values "going into" the while loop, and consider what they will do:

The first thing you're missing is "changing firstNum".

You need something along these lines as the first step to solving this problem:

There are other problems. But once you get past this first one at least your program will run. (Well.. you may have to define sumE and sumO as 0 first)
5 years ago

Junilu Lacar wrote:There's no rule that says all your tests for one particular class have to be in one unit test class. So, my approach would be to separate whitebox tests from blackbox tests. This way, if your implementation changes, only the test class with the whitebox tests needs to be changed.

I've read a bit about TDD/BDD and what not, but actually getting my design driven by tests is very difficult to actually accomplish. These tests most certainly are "verifying" types, and as such I think they are still "beginner" types of tests.

I haven't been able to really take the leap and have my design be informed by the tests yet. I feel like if I get enough practice writing tests though eventually things will click.
5 years ago
First a comment: Be careful adding "print" statements inside of classes methods. They tend not to be exactly what you want. You may come back and use the class later and be very confused at why it is printing.

There is a better solution though and it has to do with why you're seeing the strange message when printing your class.

The reason you're seeing this workbook.Contact@46e5590e is because Java interprets any custom class made into a String as a <class name>@<memory address> type of format. You can override this basic behavior relatively easily in

To do this simply override (create) the toString() method in your class. Java will automatically call this when printing or combining with other strings.

Everything that follows from this is going to assume that you've created the toString() method as I did above...

Then when you want to print out the contact you can simple do this:

This will print out the expected contact.

It is also helpful to do this because you might want to combine it with other strings/formats:

Conrado Sanchez wrote:The number of Contact that I input is just one when I run the program so the Number of contacts: 1 is correct, but it gives me workbook.Contact@46e5590e instead of printing out all the contacts stored inside the Contact class. Yes I do loop through the ArrayList and I also have a method inside the Contact class, the printNameAndPhone(), which prints out the name as well as the phone number but how do I incorporate the printNameAndPhone() method (located in the Contact class) inside the print() method (located inside the AddressBook class)???

Basically I'm asking how to access all the elements in the ArrayList<Contact> addressBook = new ArrayList<>();??

So you can use my above method, It will print out the addressBook as a list of contacts:

This is currently what you're doing. If you'd like to control how it prints more you could simple loop through the addressBook (since its a list after all)

But once again there is a better solution here I think. But this requires a bit more work on your overall program design. Which would be more in depth and may be overkill for a simple problem (if you're just trying to solve a simple problem and not trying to design an actual system).
5 years ago
Sorry in advanced. I wasn't sure how techincal of a response I'd get on my first post so I went a little abstract in the question. I feel like you all know a lot, so let me respond and then try to be more specific in my question (with more concrete code at the end).

Time Cooke wrote:It depends on what you define as a unit. If your ImplementationClass, A, B, and C all work together to perform a single 'unit' of functionality in your application then you might want to test them all together using the production versions of each class.

That makes sense. But how do I really determine what is a "unit" of functionality?

I have tried following SRP as best as I can, and as such have many smaller classes which compose other

Tim Cooke wrote:Your tests should describe a function in terms of your problem domain, not in terms of the implementation

As I am still a novice programmer I am clearly having a difficult time drawing that line in the sand. Maybe more experience will help more.

How do you normally do this? Do you utilize your API to decide what is and isn't a "unit"?

Yegor Bugayenko wrote:As a rule of thumb, try to stay as far away from mocking frameworks, as you can.

As a newbie I've been a bit of a Java "purist" with my projects. Only utilizing the standard library (because I have been trying to build comprehensive projects that showcase some knowledge). So I've never really used a mock framework (and have never developed a system so complex that I felt that I couldn't complete any given test with only 1-3 simple mocks I made by hand).

Reading your blog post you echo what I was hearing from Kent Beck and Martin Fowler in a interview I watched a couple months back. In it Beck and Fowler both seem to reel at the idea of mocking frameworks that return multiple levels of mocks. They also claim to not really use mocking at all during their unit testing because it pushes into the implementation. Having stewed on this a while I am trying to practice and write better unit tests.

Yegor Bugayenko wrote:Instead of mocking it's better to use fake objects. I've published an article on this subject a few weeks ago

I am not even understanding what the difference between the mock and fake are (except for a framework being used to generate one). I can't help but feel very confused by the fake object you're using. While it may be clear to you, I find it difficult to understand out of the context of the test.

But maybe we are having the same problem with my simple example as well

I will try to be more specific and show you what I am doing. Then you can tell me if this is a bad idea (since its hard to look at it in the abstract).

Lets use the previous example, but make it more interesting by having object interactions instead of just straight method calls. This is a more concrete example of what I am actually doing in my tests:

Lets say I have the following class I am testing (from before):

Now in this case I had simply done the following mocks:

Then my test code has a little preparation it does, but I can control what is or isn't returned by the components.

Generally that is as complex as my structure gets. I try to follow SRP enough such that I've never run into a case that I had to return multiple levels of mocks (normally just a mock that returns a stub, that stub is just confirmed that it is passed around).

Doing this I've been able to test all potential return values from expected to null and with throwing potential exceptions. But I am worried that it isn't good practice to do this.
5 years ago
Hello all. I am working on a personal Java project that is large/complex enough to need unit testing to check that it is operating as expected (and for refactoring).

This is a very general question about what is viewed as the best practice when writing tests.

Lets say I have the following interface:

Then the implementation:

Now when writing unit tests for ImplementationClass. Is it better practice to mock out each of the component classes and just test that they were run?

In doing this I seem to bind the unit tests to the implementation of ImplementationClass. When I change the implementation details of the class in the future I find that almost all of that specific unit test needs to be rewritten (since it may no longer rely on the components being used as expected).

Is it better to instead write the unit test from the prospective of the "user" and just have a simple input -> ImplementationClass -> output type of test, without any regards to what is happening inside of the class?

I find that when doing this any changes to the implementation of ImplementationClass don't really effect the test itself. But the test has dependencies on other modules (which isn't really a unit test anymore is it?)

I worry that the implementation and the unit tests are so bound up together it actually is making my test code more brittle than it needs to be.

What do you think?

This question may be entirely subjective. If that is the case, what is your opinion then on the best practice? If you were just starting on a new project and had to handle some of the original code base, which would be helpful to have?
5 years ago