How can I enclose wiring connections between a wall and a cabinet?
You have the right idea but you need a 2 gang switch/outlet box extension. They come in 1/2 increments. I prefer metal but I did see a plastic one on home depot for 3$  the metal ones run 13 to 32$.