我有以下控制器代码,我必须编写JUnit测试用例.
public class EquipmentController {
private Map<String, Equipment> equiList = new HashMap <String,Equipment>();
@RequestMapping("/rest/equipment/{Number}")
public Equipment getEquipment(@PathVariable String Number){
if(!equiList.containsKey(Number)){
lNumber = DEFAULT;
}
return equiList.get(Number);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在编写JUnit测试用例,如下所示:
import static org.springframework.test.web.ModelAndViewAssert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({/* include live config here
e.g. "file:web/WEB-INF/application-context.xml",
"file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {
@Inject
private ApplicationContext applicationContext;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
private EquipmentController controller;
@Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
// Get …Run Code Online (Sandbox Code Playgroud) 在我的控制器中,将MySQL数据库中的值放入ModelAndView object
有一个单独的程序更新表,MVC应该获取该值,因此没有表单来更新该表.
当表被更新,而当我打刷新浏览器,该值将不会在页面上更新.
调节器
@SuppressWarnings("unchecked")
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
public ModelAndView defaultPage(@ModelAttribute("user") User user) {
Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
.getContext().getAuthentication().getAuthorities();
ModelAndView view = new ModelAndView("/hello");
// Redirects to admin page if user has admin role
if (authorities.toString().contains("ROLE_ADMIN")) {
return new ModelAndView("redirect:/admin");
}
/////
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String userName = auth.getName();
User userInfo = userDAO.getUserInfo(userName);
System.out.println("Here's the thing " + …Run Code Online (Sandbox Code Playgroud) 我有一个自定义模型绑定器,用于REST API,如下所示:
public class CustomQueryModelBinder : IModelBinder
{
public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
{
if (!String.IsNullOrWhiteSpace(bindingContext.ModelName) && bindingContext.ModelType == typeof(short) && bindingContext.ValueProvider.GetValue(bindingContext.ModelName) != null)
{
short value;
var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue as string;
if (String.IsNullOrWhiteSpace(val))
{
return ModelBindingResult.SuccessAsync(bindingContext.ModelName, val);
}
else if (Int16.TryParse(val, out value) && value >= 0)
{
return ModelBindingResult.SuccessAsync(bindingContext.ModelName, value);
}
else
{
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "The value is invalid.");
}
}
return ModelBindingResult.FailedAsync(bindingContext.ModelName);
}
}
Run Code Online (Sandbox Code Playgroud)
并且在URI中未指定自定义值的情况下,它应默认为有效值(大于0),但它始终默认为0,即使控制器如下所示:
public async Task<IActionResult> GetAsync(
[ModelBinder(BinderType = typeof(CustomQueryModelBinder))]short value = …Run Code Online (Sandbox Code Playgroud) 我正在使用"laravel/framework":"4.2.*"版本,我想将模块系统用于我的项目.我已按照本文档中提供的说明进行操作.
我可以使用以下命令创建模块:php artisan modules:create module_name.我在app目录中创建了一个管理模块,并且已经创建了模块的目录结构.
我DB::select('some SQL statement')在管理模块中使用控制器的其中一个操作,但是它给了我以下错误:
找不到"App\Modules\Admin\Controllers\DB"类.
为什么找不到这门课呢?
java ×2
spring ×2
spring-mvc ×2
asp.net-core ×1
database ×1
dnx ×1
junit ×1
laravel ×1
laravel-4 ×1
php ×1