We study the solvability of the divergence equation in weighted spaces and Lebesgue spaces with variable exponents, where the weights are so called Muckenhoupt weights. The question of constructing divergence free test functions, which can be used for problems arising in fluid dynamics, is also addressed. The approach is based on an explicit representation formula for solutions of the divergence equation due to Bogovskiȋ and the theory of singular integral operators. The developed methods are used to prove an existence result for fluids which satisfy a p(·)-growth condition.