gpt4 book ai didi

java - 重定向 URL 应为 : but was in Junit

转载 作者:行者123 更新时间:2023-12-02 00:58:51 26 4
gpt4 key购买 nike

我有以下类(class):

商店.java

@Controller
public class Store {

@Autowired
private List<Products> products;

@GetMapping(value = "cart/busket")
public String busket() {
return "redirect:/cart/index";
}

@GetMapping(value = "/")
public String index(final ModelMap model) {

model.addAttribute("products", this.products);
return "product/index";
}

@GetMapping(value = "cart/buy/{id}")
public String buy(@PathVariable("id") final String id, final HttpSession session) {

//some logic

return "redirect:/";
}
//and other methods
}

Main.java - 运行应用程序

@SpringBootApplication
@Configuration
@ImportResource("classpath*:beans.xml")
public class MainApp {
public static void main(final String[] args) {

SpringApplication.run(MainApp.class, args);

}
}

StoreTest.java - 我的测试类

@SpringBootTest
@ContextConfiguration(classes = Store.class, locations = {"classpath*:beans.xml"})
@RunWith(SpringRunner.class)
public class StoreTest {

@InjectMocks
private Store myController;
private List<Products> products;

private MockMvc mockMvc;

@Before
public void setup() {
// Process mock annotations
MockitoAnnotations.initMocks(this);

// Setup Spring test in standalone mode
this.mockMvc = MockMvcBuilders.standaloneSetup(this.myController).build();

}

@Test
public void testBusket_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/cart/busket"))
.andExpect(MockMvcResultMatchers.redirectedUrl("/cart/index"));
}

@Test
public void testBuy_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("cart/buy/1"))
.andExpect(MockMvcResultMatchers.redirectedUrl("/"));
}

@Test
public void testIndex_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk());
}

}

我的 testBuy_StatusOK() 不起作用。它说:重定向的 URL 预期:“/”,但为“null”。我认为这是因为我的依赖项没有实现。我尝试了各种方法,但不起作用:\

<小时/>

更新了 StoreTest.java

@WebMvcTest(Store.class)
@ContextConfiguration(classes = Store.class, locations = {"classpath*:beans.xml"})
@RunWith(SpringRunner.class)
public class StoreTest {

@MockBean
private List<Products> products;

@Autowired
private MockMvc mockMvc;

@Before
public void setup() {
// Process mock annotations
MockitoAnnotations.initMocks(this);

// Setup Spring test in standalone mode
this.mockMvc = MockMvcBuilders.standaloneSetup(this).build();

}

@Test
public void testBusket_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/cart/busket"))
.andExpect(MockMvcResultMatchers.redirectedUrl("/cart/index"));
}

@Test
public void testBuy_StatusOK() throws Exception {

this.mockMvc.perform(MockMvcRequestBuilders.get("cart/buy/1"))
.andExpect(MockMvcResultMatchers.redirectedUrl("/"));
}

@Test
public void testGetProducts_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk());
}

@Test
public void testIndex_StatusOK() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/"))
.andExpect(MockMvcResultMatchers.status().isOk());
}

}
<小时/>

更新2

Beans.xml - 包含 beans 的文件

<beans>

...


<bean id="product1"
class="...entities.Products">
<constructor-arg name="name" value="Cherry" />
<constructor-arg name="description" value="This is very good cherry" />
<constructor-arg name="price" value="500" />
<constructor-arg name="id" value="1" />
</bean>

<bean id="product2"
class="...entities.Products">
<constructor-arg name="name" value="Cucumber" />
<constructor-arg name="description" value="This is an amazing cucumber" />
<constructor-arg name="price" value="1000" />
<constructor-arg name="id" value="2" />
</bean>

<bean id="product3"
class="...entities.Products">
<constructor-arg name="name" value="Apple" />
<constructor-arg name="description" value="This is pretty good apple" />
<constructor-arg name="price" value="3000" />
<constructor-arg name="id" value="3" />
</bean>

<util:list id="products" value-type="...entities.Products"> <ref bean="product1" /> <ref bean="product2" /> <ref bean="product3" /> </util:list>

</beans>

最佳答案

问题是 MockMvc 完全忽略测试中的 myController 变量,并使用它创建的 Controller 。

修复:

  • 从测试中删除 private Store myController;
  • 要覆盖 Controller 的任何依赖项,请使用@MockBean

此外,如果您打算仅测试应用程序的网络切片

  • @SpringBootTest更改为@WebMvcTest(Store.class)
  • 使用注入(inject)的MockMvc @Autowired private MockMvc mockMvc;

更新

如果您想在使用 @WebMvcTest 时注入(inject) @Bean,您可以在测试中提供它:

@Configuration
class ProductConfig {
@Bean
public List<Product> getProducts() {
return List.of(new Product());
}
}

@WebMvcTest(value = Store.class)
@Import({ProductConfig.class})
public class StoreTest {
}

更新2

如果你想从xml文件中读取bean,你可以使用:

@Configuration
@ImportResource({"classpath:beans.xml"})
class ProductConfig {
}

@WebMvcTest(value = Store.class)
@Import({ProductConfig.class})
public class StoreTest {
}

关于java - 重定向 URL 应为 : </> but was<null> in Junit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60969174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com