0

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)?

Akshay
  • 3,558
  • 4
  • 43
  • 77
  • 1
    You could create a MockMvc builder that is specific to your application and does all the stuff that is common to all of your tests. – Stefan Birkner Apr 13 '16 at 08:46

1 Answers1

2

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
     }
}
ndrone
  • 3,524
  • 2
  • 23
  • 37