Be careful when using the front drive shaft to assess whether 4 wheel drive engages or not. Even in a normally functioning unit, the drive shaft will spin, even in 2 wheel drive. All the shifter does is connect the transmission end of the drive shaft to an output coupling in the transmission housing. So, if there is a connection between the wheels and the drive shaft, and the tractor is moving, the drive shaft should spin.
Clemson and California already have said this, but here's a step by step procedure to isolate the trouble:
Jack the front of the tractor up, making sure everything is safely blocked. Place the tractor in 4 wheel drive.
Spin one of the front tires forward a few rotations, then backward a few rotations. The other side should spin equally but in opposite directions, and smoothly. The drive shaft should not spin. Do the same on the other side. Make sure everything feels smooth and equal side to side.
Then lower one side of the front axle until the tire is in firm contact with the ground. Pull on the top and sides of each the lifted tire to see if there is any wobble in the hub assembly. Now try to turn the tire. It should move only a fraction of a rotation, then hit solidly. You should not be able to turn the tire however hard you push. Be safe, don't knock the tractor off the jack stands.
Place the tractor in 2 wheel drive and do the same thing. The wheel should now spin freely and smoothly, along with the drive shaft. Repeat all of this on the other side, make sure to check in 2 wheel drive and 4 wheel drive.
Bowinelk had somewhat similar problems, found in this thread:
http://www.tractorbynet.com/forums/yanmar/193620-240d-front-wheel-drive-not.html
In there are the ways to tell what area of the front wheel drive system isn't working.
Don't overlook the easy fixes, though. The 186D (Very similar to your 187D) I recently picked up for my school had intermittent four wheel drive. The detent that holds the coupler for the four wheel drive engagement wasn't working right, and there was some corrosion on the sliding gears. As a result, it would pop in and out of 4wd, and give a shuddering or grinding pop, and sometimes lurch in and out of gear.
Continual heavy lubrication for the last couple weeks and a partial tear-down fixed that, and now it engages and usually holds, and disengages smoothly as well. That's why it's smart to test the front axle with one side on the ground, to see where the trouble can be localized. It wasn't super easy, but it was much better than having to do a front axle rebuild.
Good luck, as things develop keep us informed.