When to add_filter() to Custom Query

You want to add your filter before you create the query:

add_filter( 'posts_join', 'custom_posts_join' );
$test = new WP_Query( $args );

where you can define the filter callback in your theme files, for example
functions.php or in a plugin:

function custom_posts_join( $join )
{
    // modifications
    // $join = ...

    // remove the filter 
    remove_filter( current_filter(), __FUNCTION__ );

    // return the result    
    return $join;

}

Notice that we remove the filter with this line:

    remove_filter( current_filter(), __FUNCTION__ );

so it will not affect other queries.