I’m quite new to Blender.
I made a simple scene with two cubes and animated only the top cube.
It seems to work in Blender, but when I import in Unity the two cubes are rotating, not just the one with the animation.
How can I fix this?
If you don't assign some verts to any bone those verts will be assigned to the root bone in Unity. You can't make this animation works in Unity with only one bone. So add an other static bone and assign the big cube's verts to it.
Okay one thing you should know about blender is that when you create a normal transform animation and export it to an fbx, blender will apply that animation to any object that is compatible with it, so if you have two cubes and rotated one in an animation, that animation will apply to the second cube. One way to stop this is to use a rig.
Now the reason both your cubes are rotation is because you probably only parented your one cube to the bone normally and used transform animations on the bone. What you should do is parent your one cube to the bone and when the parenting options come up use "armature deform" but underneath that there should be different options, use "with automatic weights"
You will see your bone is now outlined in blue
In this mode you create your animations, after you are done creating your animations go back to object mode and export to unity and it should be working as indented