vendredi 14 septembre 2018

How to run tests from the custom jar inside maven project?

I have created custom jar with repository (MyRepo), model (MyClass) and some tests (MyTests).

This is the test class in my custom jar:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApp.class)
public class MyTests{

private MockMvc mockMvc;

@Autowired
private WebApplicationContext context;

@Autowired
private MyRepo myRepo;

@Before
public void init() throws Exception {
    MockitoAnnotations.initMocks(this);
    mockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}

And tests in that class look like this one:

@Test
@Transactional
@Rollback
public void test1() throws Exception {
    MyClass user = myRepo.find(ID);
    .....
    mockMvc.perform(post(myRequest)
                .contentType(CONTENT_TYPE)
                .content(user))
                .andExpect(status().is(MY_STATUS))
                .andExpect(content().contentType(CONTENT_TYPE))
                .andExpect(content().json(MY_RESPONSE.toString(), false));

}

I have added this jar into maven project as dependency.

Is it possible to somehow configure and change MyTests class or something inside jar, so that I can run those tests (MyTests) in the test part of the maven project, in which I have added this jar as dependency. I would like to call these test, for example, like this:

@Autowired
MyTests myTests;

.....
myTests.runAllTests();

Can anyone tell me how to do such a thing?

I want those tests from the jar to run and test my maven project and its database.

Aucun commentaire:

Enregistrer un commentaire