powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / использование WebApplicationContext в MockMvc тестах
2 сообщений из 2, страница 1 из 1
использование WebApplicationContext в MockMvc тестах
    #38415945
redwhite90
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый вечер

Есть такой тест:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class CandidateControllerTest {

	@Mock(name = "candidateService")
	private CandidateService candidateService;
	
	@Mock(name = "vacancyService")
	private VacancyService vacancyService;
	
	@Mock(name = "utilService")
	private UtilService utilService;
	
	@Mock(name = "eventService")
	private EventService eventService;

	@InjectMocks
	private CandidateMenuController candidateMenuController = new CandidateMenuController();

	MockMvc mockMvc;

	@Before
	public void before() {
		MockitoAnnotations.initMocks(this);

		mockMvc = MockMvcBuilders.standaloneSetup(candidateMenuController).build();

	}

	@Test 
	public void testgoToCandidateMenuMockMvc() throws Exception { 
			
		MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/goToCandidateMenu");
		ResultActions result = mockMvc.perform(request);
		result.andExpect(status().isOk());
		//result.andExpect(content().contentType(MediaType.TEXT_HTML));
		result.andExpect(MockMvcResultMatchers.view().name("candidateMenu"));
		//result.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/candidateMenu.jsp"));
		
	 }
	

	@Test
	public void testgoToCandidateMenu() throws Exception {
		String viewName = candidateMenuController.goToCandidateMenu();
		assertEquals("candidateMenu", viewName);
	}
}



так у меня всё работает, но я хочу использовать WebApplicationContext и пишу так:



@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class CandidateControllerTest {

@Mock(name = "candidateService")
private CandidateService candidateService;

@Mock(name = "vacancyService")
private VacancyService vacancyService;

@Mock(name = "utilService")
private UtilService utilService;

@Mock(name = "eventService")
private EventService eventService;

@InjectMocks
private CandidateMenuController candidateMenuController = new CandidateMenuController();

@Autowired
WebApplicationContext wac;

MockMvc mockMvc;

@Before
public void before() {
MockitoAnnotations.initMocks(this);


this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();

}

@Test
public void testgoToCandidateMenuMockMvc() throws Exception {

MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/goToCandidateMenu");
ResultActions result = mockMvc.perform(request);
result.andExpect(status().isOk());
//result.andExpect(content().contentType(MediaType.TEXT_HTML));
result.andExpect(MockMvcResultMatchers.view().name("candidateMenu"));
//result.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/candidateMenu.jsp"));

}



}





такой трейс:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
java.lang.AssertionError: Status expected:<200> but was:<404>
	at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
	at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
	at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:549)
	at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
	at controllers.CandidateControllerTest.testgoToCandidateMenuMockMvc(CandidateControllerTest.java:118)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)




Помогите понять что делаю не так.
...
Рейтинг: 0 / 0
использование WebApplicationContext в MockMvc тестах
    #38415947
redwhite90
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
@ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class CandidateControllerTest {

  @Mock(name = "candidateService")
  private CandidateService candidateService;

  @Mock(name = "vacancyService")
  private VacancyService vacancyService;

  @Mock(name = "utilService")
  private UtilService utilService;

@Mock(name = "eventService")
private EventService eventService;

  @InjectMocks
  private CandidateMenuController candidateMenuController = new CandidateMenuController();

  @Autowired
   WebApplicationContext wac;

  MockMvc mockMvc;

  @Before
  public void before() {
  MockitoAnnotations.initMocks(this);


  this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();

 }
  @Test 
  public void testgoToCandidateMenuMockMvc() throws Exception { 
  MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/goToCandidateMenu");
   ResultActions result = mockMvc.perform(request);
  result.andExpect(status().isOk());
 
  result.andExpect(MockMvcResultMatchers.view().name("candidateMenu"));
 }
}
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / использование WebApplicationContext в MockMvc тестах
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]