Akshay
Akshay

Reputation: 3866

Unit testing spring mvc with spring-test and junit

I am new to unit testing with spring-test. I have a spring-mvc-rest application. I am able to mock the environment using MockMvc.

My question is do I need to build the MockMvc object in every class of testing? Would that not be repetitive configuration.

Is there a way to define this in one class and use it across every testing class?

If we go by single configuration or multiple configuration then which is the best way (by design and by maintenance)?

Upvotes: 0

Views: 292

Answers (1)

ndrone
ndrone

Reputation: 3582

It's called inhertance

for example

Base Class

@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
public class BaseTest
{

    protected MockMvc mockMvc;

    @Before
    public void setup()
    {
        mockMvc = MockMvcBuilders.standaloneSetup().build();
    }
}

Extend

public class ExtendedTest extends BaseTest
{

     @Test
     public void test()
     {
       //execute test here we have access to the mockMVC
     }
}

Upvotes: 2

Related Questions